Type Conversions in Kotlin

Start course
1h 43m

This course covers the fundamentals of Kotlin, looking at the building blocks of the programming language and how they can be used to build apps in Android.

Intended Audience

This course is ideal for anyone who wants to learn how to use Kotlin for developing applications on Android.


This content will take you from a beginner to a proficient user of Kotlin and so no prior experience with the programming language is required. It would, however, be beneficial to have some development experience in general.


All right. Hello, my friends. So, in our previous lesson, we have learned the data types that are used in Kotlin. But we haven't learned any string and array data types yet. Well, we're going to learn more about this data type in our next lessons. But, in this lesson, we got to first learn how to convert between data type. Wait, what?

Why would we need to convert data types? Why do we need data conversion? Okay, before you ask too many questions, let's say that you're going to develop a calculator application and you'll divide the one integer number entered by the user by another integer number entered by the user. But the result of the operation may not always be an integer. So, in this case, if you don't convert the result to a double or a float, you're going to lose data.

So, if it doesn't quite make sense, let me just make it concrete with an example. So, the user wants to divide the number 25 by two. So, in this case, the result is going to be 12.5. If you define the result variable as an integer rather than a double or a float, well, in Kotlin, it's going to round it to 12. So in this case, we could use some type conversion. So, how do we do that? Okay, you got me convinced. Let's do it. Well, but first, before we jump in, there's a number of different methods using Kotlin for this. That's why we're taking so long to delve in.

So, have a look at these methods now. To convert any data type to byte type, the toByte() method is used. To convert any data type to short, the toShort() method is used. To convert any data type to the integer type, the toInt() method is used. You following?  So, to convert any data type to the long type, the toLong() method is used. And any data type to the float type, the toFloat() method is used. And to convert any data type to double, the toDouble() method is used.

Now of course, there is another thing that you got to pay attention to. So, you remember, I shared the sizes of data types in the previous lesson? So, let me just remind you once again here, the largest of the data types is double, then float, then long, then integer, then short, then byte. So, if you convert a small value to a large value, there's not going to be a problem. In other words, if you convert a byte type to a double type, you will not experience any data loss.

However, if you convert a large value to a small value, you may indeed incur data loss. For example, if you convert a variable of the type double to type integer, you will most definitely lose data. All right, my friends. So, that's a little bit of inside info about this type conversion, but why don't we put it to good use and let's open up Android Studio and do some practice. So, first I want to create a new Kotlin file. Now you could continue on the previous Kotlin file, but since I'm going to share these examples with you at the end of the section, I just want each topic to be a separate file. So, you can follow the code according to the subject order, and some people just like it that way. So, I will set the file's name as type conversion. So now, let's create the main method. So, first of all, for your convenience, I will write out the type conversion methods in the comment line:  toByte(), toShort(), toInt(), toLong(), toFloat(), and finally, toDouble(). All right. So, now I can create a variable of the byte type here. So, I'm typing 'var x:Byte = 128'. As you can see, Android Studio gives a warning. So, why? Anyone? Anyone?

All right. Let me just tell you real quickly. A variable of the type byte could have a maximum value of 127. Therefore, we wouldn't be able to give this variable value of 128 or more. So, I'll just fix this and type in 127 here. Okay. So now, let's create a variable of the type integer and pass the value of the variable x. All right? I'm typing 'var y:Integer' equal, you are right, 'x.toInt()'. See how that converts the x value of the type byte to type integer. All thanks to the toInt method, and it transfers it to the variable y. Sounds great, a variable of the type double and pass the value of the variable y. So, I'm typing 'var z:Double';  I'm typing equals 'y.toDouble()'. And that way, we convert the y value of the type integer into double thanks to the method of type toDouble()  and transferred it to the variable z.

So now, we can print the x, y, and z variables to the console. So, we will print the x value first. So, just type in 'printIn x'. Now I can copy and paste this line twice, and I'll write y here and z here. Now let's run our code. As you can see on the console screen, the value of the x my variables is printed as 127. The value of the z variable is 127.0. So actually, what we did here was type conversion from small to large. So, we didn't lose any data, but I do want to show you type conversion from largest to smallest. Let's see what happens.

First, I'll just convert these lines of code into the command line. Now I'll create a variable of type double. Typing 'var a:Double = 132.32'. Just below the line, I'm typing 'var b:Integer = a. toInt()'. Last but not least, I'm typing 'var c:Byte = b.toByte()'. And now let's print these values to the console. So, I'm copying pasting the above printIn methods here. But instead of x, y, and z, I'lI write a, b, and c. All right. So now, let's run our code. So, what you see here is the value of a is 132.32. While the value of b is only 132. So, the value of c is a very different result friends. That's because we tried to convert a large number into a small number, and what did I tell you? We've got some data loss. So, you've got to be forward thinking here and take it into consideration in future applications because it could turn around and bite you where you don't want it to bite you. All right, my friends. So, that is type conversion in Kotlin. We'll take a short break here and then, in the next lesson, we're going to learn about that string data type that I promised you about. All right. So, I want to see in the next one.


About the Author
Learning Paths

Mehmet graduated from the Electrical & Electronics Engineering Department of the Turkish Military Academy in 2014 and then worked in the Turkish Armed Forces for four years. Later, he decided to become an instructor to share what he knew about programming with his students. He’s currently an Android instructor, is married, and has a daughter.

Covered Topics