7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.4 Variables and Data Types

Hey there, I hope you all are doing well. In the previous lessons, while we were learning about I mentioned about the var keyword. Let's say we need a variable element. We will define it as var, which is a keyword for variable. And we will define the variable name, element. And here we have an error. It says this variable must either have a type annotation, or should be initialized. Why does it say so? Because we don't have any default value for any of the data type in Kotlin. Like in Java, we had the default values for all the data types. There is no such ceremony in Kotlin. Hence, we need to either initialize this variable element, or we need to define the type of value it will carry with it. That is, the data type for the variable. Let me first show you how we define the data type for the variable. We have the variable name. Then we will put a colon, followed by the type of the data this element variable will have. Let us say it is string. So this is the string type variable. Now when we print this element, it says variable element must be initialized. As I already said, there is no default value for the variables of any data type in Kotlin. We need to initialize element. Let's say it is a circle. Now when we print element, let's see what's the output. And here we have circle in our output. This element variable here is Mutable. Why we call it Mutable because we have the element initialized here as circle. And then we can reuse this variable to have some other values in it. Let's say it is a square and then again, we write println(element). And then when we execute our code, we get circle as well as square in the output console. Here we are. Hence we can see that this element variable is mutable, which I have used with the var keyword. Let us first jump into the details of the data type in Kotlin, and then we will see what is mutable kind of variable, and what is the other kind that is immutable variable type in Kotlin? Let's see the data types in Kotlin first. The data types in Kotlin are objects. Whereas in Java, we had the data types as the primitive data types. In Kotlin, the data types are Boolean, byte, char, short, int, long, float and double. Since they are objects, we can figure it out that each of them starts with an uppercase letter. We see b in Boolean, b in Byte, c in Char, s in Short, and so on. Whereas in Java, we had the data types as the primitive data type. May be used to write Boolean, may be used to be in the lowercase. Same for the respective data types. How much memory does each of them consume? We have 64 bit for double, 32 for float. Long has 64 bit. Int, 32, short, 16, char, 16, byte, 8 and Boolean, 1 bit. The ranges for each of them are as follows. Boolean can be true or false. Byte has a range from -127 to 128. Char can be any character a to zed. It can be any numeric value or the special character enclosed within single quotation. This way there are ranges for each of the data type. And to be specific, when it is double, the value should be followed by small d or capital D. If it is flawed, the value should be followed by f, or capital F. For Long, it has to be a capital L. For int, we don't need any search characters to be followed by the integer value. After all this, we need to keep in mind that all variables should be initialized when it is Kotlin because they don't have any default values. Coming to the IDE again, let us see how we can define other data types. Say we need the elements side. Let's say it's of integer type. Keep it as 3. So here we have the var keyword side is the variable name. Int is the data type, and here we have initialized its value. Instead of keeping it in two line, we can mention it in the same line. For float, here it says the floating point literal does not conform to the expected type float. I mentioned earlier, if it has a decimal point, the default data type is double. So we need to mention it explicitly that it is offload type. To do that, f to the value. For Boolean, so here we are with different data types in Kotlin. Let's jump into variables and constants, and then var and val. Earlier I had mentioned that this element is mutable, which has been declared with the var keyword. Now, how do we define constants in Kotlin? Let me define any of the variable. Let's say val figure. This figure variable has been defined with a keyword val, and let me give it the same string type, and initialize it as rectangle. And now if I again initialize the figure as, let's see, triangle, like I have done for the element, it gives me an error. It says val cannot be reassigned. Hence this figure variable that has been defined with the keyword val is immutable. So we have the difference between the var and the val keyword. The variable defined with the val keyword is immutable and is a constant. Whereas if we have a variable defined with the var keyword, it does Mutable and can be reinitialized with some other value. So here we are with the data types and variables in Kotlin. Let us remove this and let's play around with the print statement. I want to print the area of the square. So let's run our code and see what happens. Here we are. We get the area of the square is 9. So that's all for this lesson. In the next lesson, we will learn about functions and classes in Kotlin. So stay tuned, and have a good day.

Back to the top