Data Types in Android 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.


Hello, my friends. So, in this lesson we're going to go over some data types. The data type basically defines the values that a variable can take. Based on the data type of a variable, the operating system allocates memory and then decide what can be stored in the reserved memory. So therefore, by finding different data types to variables, you could store integers, decimals, or characters as these variable. So, in common, we generally define the data type of the variable to use it and we cannot assign incompatible data type, otherwise the compiler will give us an error. So, let's have a look at this diagram.

Now there are five basic data types available in common. First one is Boolean data types. The second is character data types, and this is represented in common as char. Now the third is a numbers data type and there are two different number variables. So, the first is integer variable and the second one is floating point variables. There are four different integer variables, and these are byte, short, int, and long variables from smallest to largest.

Floating Point variables are also divided into two. These are float and double. And then finally we'll get to string variables and these represent textual expressions and a raise, and these can hold more than one value in themselves. All right, so let's have a look at the different column data types in this table. The byte data type is an 8-bit signed two's complement integer. The byte data type is useful for saving memory and large arrays, as a minimum value of -128 and a maximum value of 127 inclusive. So, the default value of the byte is zero. The Short data type is a 16-bit signed two's complement integer. Similar to byte, uses a short to save memory in larger arrays in situations where the memory savings actually matters.

Now this has a minimum value of -32,768 and a maximum value of 32,767 inclusive. Also the default value of short is zero. The int data type is a 32-bit signed two's complement integer which has a minimum value of -2^31 and the maximum value of 2^31st -1. The long data type is a 64-bit two's complement integer. The sign long has a minimum value of -2^63 and a maximum value of 2^63 -1. So, you use this data type when you need a range of values wider than those provided by int. Float data type is a single precision 32-bit floating point. So, if you need to save memory in large arrays of floating point numbers, definitely use a float instead of a double. The double data type is a double precision 64-bit floating point for decimal value. This data type is generally the default choice.

Boolean data type will represent only one bit of information, either true or false. The char data type is a single 16-bit unicode character. All right, so when defining data types in Kotlin, we've got to first specify the variable type. Then, we write the name of the variable that you're going to create. So, in this example the name of the variable is age. Then we have to specify the data type after putting in a colon. So, in this example the data type is integer.

Okay, so after the equal sign, we've got to pass the value of the variable. So, in this example I have passed the value 20 to the age variable of the type, integer. Okay, so that's how we define a variable in Kotlin. All right, so that's marvelous information that hopefully you didn't know before about using variables with Kotlin. So we'll move on to Android Studio and do a little bit of an exercise. So, we first got to create a new Kotlin file. So I'll just right click on the package folder in the project directory and select new Kotlin file. Now I' specifying the name of this file is data types. When you press 'Enter' on the keyboard, our kotlin file is created, okay? So, of course you remember that we need at least one main method in Kotlin. First let's create the main method. So, we'll start with data types, var a:Boolean = true. So, this Boolean type has two possible values, either true or false. var b:Char = 'R'. Char type declares a character var c:Byte= 12. So, this byte type can have values

between -128 right the way through 127. var d:Short = -356. So, the short type can have values from -32,767 right the way through 32,767. var e:Int = 43543. Int type is generally used for numeric values. So, now let's declare variable f with the long data type. Long type is used for large values, as I'm sure you're remembering. So, this capital L is a specific suffix for the long type. Another variable g with float data type, and float type is used for large values with single precision. And capital F is the specific suffix for float type. Now on other variable h with double data type. So, double data type is used for large values with double precision. So, there we go. We've defined the variables, we've set their initial values. So, now let's print the values of these variables on the console just by using the println() function.

So, first I will print the a variable to the console. Now I'll copy this line of code by pressing 'Ctrl + C' on the keyboard at the same time, paste it seven times by pressing 'Ctrl + V' on the keyboard at the same time. And let's change the variable name from a to h. All right, so now let's run the code. Let's see the results in the console. Very cool, my friends. So, we're going to end this lesson here and next time we meet, we will learn type conversion and type casting. All right, so I'll see you in the next video.

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