Kotlin Type Conversion
Type conversion is a process to modified one data type value to another data type. Or we can say that changing the behaviour of value. Such as storage capacity, memory capability and working functionality etc. Kotlin programming is provide few standard methods to modified one valid data type to another data type.
Type conversion methods
toByte()
This method are used to convert other data type to byte numeric values. We can modified any value which are ASCII value in between (-128 to 127) range. Compiler are capable to convert any value to short data type there ASCII are exist between of given range. For example.
fun main(args: Array<String>) {
println('Z'.toByte()) //convert char to bytes
println(101.toByte()) //convert int to bytes
println(0b010101.toByte()) //convert binary to bytes
println(120.0.toByte()) //convert double value to bytes
}
Output
90
101
21
120
toShort()
Range of sort data type are (-32768 to 32767) so we can capable to convert any variable and constant letterals, those are not break data range.
fun main(args: Array<String>){
println("1000".toShort())
println(0b111.toShort()) //binary literals
println(56.2.toShort()) //double literals
}
Output
1000
7
56
toInt()
fun main(args: Array<String>){
println('A'.toInt())
println("10".toInt()) //String literals
println(0b1010.toInt()) //binary literals
println(10.2.toInt()) //double literals
}
Output
65
10
10
10
toLong()
fun main(args: Array<String>){
println('P'.toLong())
println("1111".toLong()) //String literals
println(0b1111.toLong()) //binary literals
println(1111.222.toLong()) //double literals
}
Output
80
1111
15
1111
toFloat()
fun main(args: Array<String>){
var text:String="44"
var num:Int=100
var alphabet:Char='A'
var check:Float=text.toFloat()
println("text : $text Type : ${text.javaClass.kotlin}")
println("check : $check Type : ${check.javaClass.kotlin}")
check=num.toFloat()
println("num : $num Type : ${num.javaClass.kotlin}")
println("check : $check Type : ${check.javaClass.kotlin}")
check=alphabet.toFloat()
println("alphabet : $alphabet Type : ${alphabet.javaClass.kotlin}")
println("check : $check Type : ${check.javaClass.kotlin}")
}
Output
text : 44 Type : class kotlin.String
check : 44.0 Type : class kotlin.Float
num : 100 Type : class kotlin.Int
check : 100.0 Type : class kotlin.Float
alphabet : A Type : class kotlin.Char
check : 65.0 Type : class kotlin.Float
toDouble()
fun main(args: Array<String>){
var text:String="53"
var num:Int=23
var alphabet:Char='A'
var check:Double=text.toDouble()
println("text : $text Type : ${text.javaClass.kotlin}")
println("check : $check Type : ${check.javaClass.kotlin}")
check=num.toDouble()
println("num : $num Type : ${num.javaClass.kotlin}")
println("check : $check Type : ${check.javaClass.kotlin}")
check=alphabet.toDouble()
println("alphabet : $alphabet Type : ${alphabet.javaClass.kotlin}")
println("check : $check Type : ${check.javaClass.kotlin}")
}
Output
text : 53 Type : class kotlin.String
check : 53.0 Type : class kotlin.Double
num : 23 Type : class kotlin.Int
check : 23.0 Type : class kotlin.Double
alphabet : A Type : class kotlin.Char
check : 65.0 Type : class kotlin.Double
toChar()
fun main(args: Array<String>){
var num:Int =65
var text:Char=num.toChar()
println("num : $num Type : ${num.javaClass.kotlin}")
println("text : $text Type : ${text.javaClass.kotlin}")
}
Output
num : 65 Type : class kotlin.Int
text : A Type : class kotlin.Char
toString()
This method are used to convert non string variable and value to string type
fun main(args: Array<String>){
var num:Int =10
var salary:Float=1000_00.5f
var text:String=num.toString()
println("num : $num Type : ${num.javaClass.kotlin}")
println("text : $text Type : ${text.javaClass.kotlin}")
text=salary.toString()
println("salary : $salary Type : ${salary.javaClass.kotlin}")
println("text : $text Type : ${text.javaClass.kotlin}")
}
Output
num : 10 Type : class kotlin.Int
text : 10 Type : class kotlin.String
salary : 100000.5 Type : class kotlin.Float
text : 100000.5 Type : class kotlin.String
Kotlin Type Checking
Type checking is process to find the variable value type in runtime. is and !is are used to check data type. For example.
fun main(args: Array<String>){
var i: Int =3
var text:String ="Code"
if (i is Int){
println("Integer Type")
}else{
println("Not Integer Type")
}
if(text !is String){
println("Type is Not String")
}else{
println("Type is String")
}
}
Output
Integer Type
Type is String
TNote that inside a if statement. when we are check check data type by using is or !is keyword then compiler are produced an error in this situation. In this case we can declared data type of variable is Any type. There is not good way to check data type of variable in if statement. when statement is suitable if check multiple data type of single value or variables.
fun main(args: Array<String>){
var data:Any ="Text"
when (data){
is Int -> println("Int Data Type")
is Char -> println("Char Data Type")
is String -> println("String Data Type")
else -> println("Other Data Type")
}
}
Output
String Data Type
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment