Type casting is a way of converting a variable of one type to another type.
Let us first go through some basic questions in order to understand Type casting better.
What happens when a variable is declared ?
Object of a specified type is created and the variable will hold reference to that object.
Example :
var x = 6
Explanation :
Value assigned is of type Int therefore object of type Int is created and the reference is held by variable “x”.
What types of objects are created?
Based on the value assigned, they can be of the following types
Value assigned | Object type |
6, 48, 3.12 – Numeric values | Numeric object |
‘a’, ‘m’ , ‘z’ – Character value | Character object |
“welcome”, “to”, “technical”, “trials” – String value | String object |
true, false – Boolean value | Boolean object |
what is type used for?
Type is used to decide operations that can be performed on the variables
Example :
Object | Operations |
String object | combine two strings, convert it to capital letters |
Numeric object | basic math operations, convert to different numeric type |
Type casting is done on which data types ?
Numeric types
How is Typecasting done ?
By calling one of the following methods using a Dot operator ( . )
Method | Purpose |
toByte() | To convert a variable to Byte |
toShort() | To convert a variable to Short |
toInt() | To convert a variable to Int |
toLong() | To convert a variable to Long |
toFloat() | To convert a variable to Float |
toDouble() | To convert a variable to Double |
Example :
fun main (args:Array<String>)
{
var x = 10
var z : Long = x.toLong()
println("Value of x is "+x)
println("Value of z is "+z)
}
Output :
Value of x is 10
Value of z is 10
Explanation :
variable x = 10, as Int value is assigned a Int object is created and the reference of this is held by variable x.
var z : Long = x.toLong()
toLong() function is called on variable x using the dot operator and the result is assigned to variable z
This creates a new object of type Long and the reference is held by variable z.
what is overspill?
Overspill is when you try putting very large value into a smaller data type
Example :
fun main (args:Array<String>)
{
var x = 1234567890123
var z : Int = x.toInt()
println("Value of x is "+x)
println("Value of z is "+z)
}
Output :
Value of x is 1234567890123
Value of z is 1912276171
Explanation :
var x = 1234567890123, based on the value assigned Long object is created and reference held by variable x
x.toInt() – trying to convert variable x of type Long to Int by calling toInt() using a dot operator
which is then assigned to var z : Int, which is explicitly declared as Int
the resulting value should have been 1234567890123 but as you are trying to assign a very large value into a smaller data type
there will be over spill and the variable would have weird value 1912276171
what if we want to assign multiple values to a variable ??
Example :
fun main (args:Array<String>)
{
var x = 1,2
println("Value of x is "+x)
}
Output :
Kotlin: Unexpected tokens (use ‘;’ to separate expressions on the same line)
Explanation :
var x = 1,2 – > variable can hold single value as we have tried assigning two value 1 and 2, the compilation gives error
If you follow the output message and try separating the values with “;” it would print only the first value
Then how do we handle a scenario, where in we have to deal with multiple values ??
That is when we go for using type of object in Kotlin know as Array’s .