Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




KOTLIN - TYPE CASTING


So far we have seen, how to add two numbers that are of the same type.


i.e. We have added two numbers that are of Data Type 'Int'.


Example :



fun main(){
	
    var x: Int = 5 
    var y: Int = 6
    var z: Int = x + y
    println("The added value is : "+z)
}  


Output :



 The added value is : 11

So, if we see the above output, we got the correct added result (i.e. '11') because the Data Type of 'x' and 'y' is 'Int' because both have an integer value in it.


var x: Int = 5
var y: Int = 6

Similarly, if we see the the variable 'z', it is also 'Int'. Because 'z' holds the added value of the variables 'x' and 'y'. Which is also an integer(i.e. 11).


Now, there might also be a scenario where you want to add a floating point number and a whole number. And get the result as a whole number.


i.e. We want to add the whole number '5' and a floating point number '6.5'. And the output we want should be 11 and not 11.5.


Let us see in the below example.


Example :



fun main() {
	
    var x: Int = 5 
    var y: Float = 6.5F
    var z: Int = x + y
    println("The added value is : "+z)
}   


Output :



 Type mismatch: inferred type is Float but Int was expected

Well! If we see the output! It says, Type mismatch


Which in simple words means, a floating point number (i.e. '6.5') and and a whole number (i.e. '5') when added cannot produce an Integer.


Now, if we see the Data Types of all the variables :


For variable 'x', it is 'Int'.

For variable 'y', it is 'Float'.

For variable 'z', it is 'Int'.


Which says, 'z' cannot hold an Integer.


So, how do we solve this problem?


And this is where, 'Type Casting' comes into picture.


Let us rewrite the above program with Type Casting.


Converting a Float Data Type to an Int Data Type


A solution to it would be, to convert the field 'z' to 'Int'.


Example :



fun main(){
	
    var x: Int = 5 
    var y: Float = 6.5F
    var z: Int = (x + y).toInt()
    println("The added value is : "+z)
} 


Output :



 The added value is : 11

Now, if we see the output,


The added value is : 11

We can see the added value of '5' and '6.5' is '11' and it didn't result an error.


And how did we achieve this?


Well! By Type Casting.


Now, if we see the above code, we have assigned the number '5' to a variable 'x'.


var x: Int = 5

And the floating point number '6.5' to variable 'y'.


var y: Float = 6.5F

Now, we don't want the added result as '11.5' and just '11'.


We have used 'toInt()' function to convert the added value to 'Int'.


var z: Int = (x + y).toInt()

'toInt()' function is used to convert the added value to an Integer data type.


(x + y).toInt()

And now since the added value is converted to 'Int' Data Type, there is no problem


Just like 'toInt()' method, that is used to convert a data type to 'Int'. There are other methods, that is used to convert a Data Type to another.





Function Description
toInt() Converts a Data Type to Int Data Type
toLong() Converts a Data Type to Long Data Type
toByte() Converts a Data Type to Byte Data Type
toShort() Converts a Data Type to Short Data Type
toFloat() Converts a Data Type to Float Data Type
toDouble() Converts a Data Type to Double Data Type
toChar() Converts a Data Type to Char Data Type