Kotlin Type Conversion

Introduction to kotlin

Type conversion

  • Type conversion is also called as type casting.
  • Type conversion is refers to chnaging the entity of one data type to another data type.
  • Kotlin does not support implicit type conversion.
  • In Kotlin helper function used to explicitly convert one data type to another data type.

Example

var myInt : Int = 100
var myLong : Long = myInt.toLong()

In above example you can see Integer value converted into Long value

Helper Functions

  • toByte()
  • toShort()
  • toInt()
  • toLong()
  • toFloat()
  • toDouble()
  • toChar()

Using this member function we can convert one data type into another data type.

Example

fun main() {
var myDec : Int = 65
var mychar : Char = myDec.toChar()
println(myDec) //65
println(mychar) //A
}

In above example we store decimal value of ascii character 'A' in myDec variable. after that we converted this value in character whitch is 'A'.

Comments