Variables in Kotlin

Variables in Kotlin
Overview
Difficulty
Beginner
Duration
1h 43m
Students
23
Ratings
5/5
starstarstarstarstar
Description

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.

Prerequisites

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.

Transcript

Hello everyone. So, in this lesson we are starting the main fundamentals of this Kotlin programming language, which is why you signed up. So, one of them is variables. So, in this section we're going to first learn, what is a variable? But then, we're going to learn how to declare and initialize variables. See, that way we'll be learning types of variables because there are mutable variables and immutable variables, and everything is variable. All right? So, let's start off with this variable question. So, if you think of variables like a container or a box, which holds a value for you during the life of your particular Kotlin programme. So, we'll keep it relevant. So, what it means is once you create a variable, you can reserve some space and memory. So, every variable is assigned a data type. And then, it holds the quantity of value or whatever the heck you're putting in there. I'll show you why I say that, because there are two different steps to use a variable in a programme. All right? Declaration and initialization. So, in order to declare a variable, you must specify the variable and give a unique name to the variable.

So in our example, we'll declare the variable to be age. Now, in order to initialize a variable, you've got to assign it a valid value according to its data type. So, we use the =, means that the value on the right side, the = will be saved into the variable on the left side. Found? So in our example, we assign the value 37 to the variable age. So, I want to tell you, though, that this Kotlin programming language has rules and conventions for naming variables. So, while declaring variables, please note that Kotlin is a case sensitive programming language. That means that upper or lower case letters in your Kotlin programmes matter, matter a lot. So, this variable age, that here, consists of all lowercase letters, is way different from a variable age, which would be the first letter capital, and the rest, lowercase. So, to continue with these naming conventions and rules, a variable name can begin with a letter or an underline mark. If you declare a one-word variable name, use lowercase all letters. This isn't necessarily mandatory, but it is conventional wisdom.

Also, if you declare a variable name having more than one word, use all lowercase letters for the first word and capitalize the first letter of the next word. So, let's get into the types of variables. So, we have two different types of variables in common. Mutable variables are represented by var, immutable variables are representatives val. So, basically, val and var are both used to declare a variable, but var is like a general variable and can be assigned multiple times. This is also known as the mutable variable in Kotlin, whereas val is a constant variable. How's that? And cannot be assigned multiple times. And can be initialized only one single time. So that's why this is known as the immutable variable in Kotlin. All right? val, the immutable variable in Kotlin; sounds like a hero. But we're not doing a complex game right now, so we'll postpone that idea.

So, enough about variables, why don't we just move on to Android studio and do a little bit of an exercise? This way, you'll get used to everything about it. So first, we'll create a new common file. For this, all I have to do is right click on the package folder in the project directory, and select 'New', 'Kotlin File', and follow the options. So, I'm specifying the name of this file to be variable. So, when you press 'Enter' on the keyboard, the Kotlin file is created.

Now, remember we needed at least one main method in Kotlin. So first, let's create the main method. But this time I want to show you how to create the main method in an easier way. All right, so I'll just type in 'main' here. From here, I'll select the 'maina' option and press 'Enter'. And what do you see here? We created the main method right away. All right, so now, we can get to coding. So, we'll declare three variables. First, I'll write var a, and assign a value of 10. Second, b, and assign a value of 20. And lastly, c, and assign a + b. Okay, so let's print variable c on the screen just by using the print method. Now, Android studio, it has a quick shortcut for you to write this method. So, just type sout, and then hit 'Enter' on the keyboard.

So, this is named code completion. So, it allows us to quickly complete statements in our code. We can use this combination, and well, all of the other parts of this programme; variables, functions, classes, etc. Also, notice that in our previous lesson, we used only the print function when printing Hello World to the console. But here, we're using the println(). So, this means that after we print the desired expression of the console, the cursor then automatically moves to the next line. That is, when we print more than one result to the console using the println method, the results are listed one below the other. So, let's run the code. For this, right click on the 'Variable.kt' file and select 'Run'. And the result will print 30 in the console.

Also, we can directly write a + b to the output. Okay, so let's run it again. And the result is correct. Very good. So, let's look at some of the naming rules for variables. So, we're going to study this via variable age, var age is valid. So also, I would like to mention the comment line in Kotlin here. So, in this Kotlin programming language, if you use a //, all the statements you type from that point on through to the end of the line are perceived to be comments. It is. It's not perceived as code. Comment lines are not taken into account when compiling your code. If you want to use more than one line as a comment line, well, you can write whatever you want between /* and */ this time. So, just by using this feature, you can take notes on the margins of code here. Let's continue from where we left off. var _age is valid. var age3 is valid. But var a ge is invalid. And var 3age is invalid. All right? Because a variable name cannot contain a space, as well as, you can't begin a variable name beginning with a number. All right, so let's comment out our code here.

So, I'll use the / and * marks for the comment line as there are multiple lines. So now, I want to show you the difference between var and val. So first off, I'll create a variable of type var named age and assign the value 20. All right, now I'll assign the value 10 to the age variable in the next line. And now, let's print the age variable to the console. So, what do you think is going to be written on the console? You must have an idea. 10 or 20. So, those who said 10, you got it. Because the type of the age variable is var, and we can change the value of this variable type var. We first transfer the value 20 to the age variable. Then, we transfer the value 10 in the next line. And as a result, the value of the age variable became 10 and we printed 10 to the console. So now, let's create a variable of the type val. So, I'm typing 'val year', and  I'll  assign the value 2020 to the year. So, let's try to change the value of the year variable in the next line. So, I will write year = 2019. So, as you can see Android Studio throws an error because when we position the cursor over the word year, we see the warning val cannot be reassigned. So in other words, we wouldn't be able to pass a second variable to a variable of the type val. All right. So, what's going on here?

When do we need the val? And when do we need the var? Well, while developing an application, we may want the value of some variables to remain the same. For example, if you're going to make a calculator and you're going to use the value pi, and you're going to specify pi to be three all over this application, on this case you could create a variable of the type val named pi. Or we could think of it like this. Let's say you've developed an application that retrieve some data from the Internet. So in this case, the data retrieved from the Internet may vary from time to time. So in that case, you would definitely specify the type of the variable that you would define to be var. So, that's the concept of var and val. I hope you understood them. Because now, you really just need to play with them and write them in and see how you do. But now you know what you need to do in order to create a variable. All right. So, in our next lesson, we're going to learn about data types. So, see you there in the next video.

 

About the Author
Students
97
Courses
23
Learning Paths
1

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