Learn Kotlin: Variables
Ahh, yes. Variables, one of the most important concepts in a programming language.
If you have some experiences in coding before, you might have an idea why I put that pic on the left. But if you are new and still confused, don’t worry because you will get it in a second. So, let’s just jump right into it. Shall we?
Often times when you are coding, you will have to deal with data. You will have to deal with data from your application input, process, and output. And it will be extremely exhausting if you don’t know how to reference to a certain data that you need in your program. That’s where variables come in handy, it will alleviate your pain from typing the same data over and over again by just referencing it whenever you need it.
So with that being said, I present to you … *drum roll*
Variables are used to store information or data values which you want to use later in your program. The easiest and most mainstream way to wrap your head around variable is to think of it like a box.
A variable is just like a box that can hold a single thing. You need to give a name for the box to help you reference to it. In the above illustration we have three boxes, each of them has their own name and a thing to hold. The box named myName hold a value of “Bob”, myAge hold value of 17, and isHappy hold value of true.
1. Variable declaration
Let’s see how we declare variables in Kotlin. Here is the base syntax to declare a variable :
var variableName: DataType = value
Here are the examples of how you might declare variables based on the boxes illustration above :
How do you read these codes? Well, let’s take example from line 1 of the code. This is how you might want to comprehend it in english:
myName is an immutable variable of type String which hold the value “Bob”
Important things to note here :
- val and var are keywords to declare a variable. This has something to do with something called mutabality. What is the difference between var and val? What does it mean to be mutable? We will talk more about that after this.
- String, Int, and Boolean are data types. We will talk more about it in the next lesson.
- “Bob”, 17, and true are called values. Different values can only be assigned to a corresponding data types. This will also be covered later when we talk about data types.
2. Mutability of a variable : var vs val
The difference between var and val :
- when you use val for a variable declaration, that variable will be immutable, the value of that variable can only be assigned once, it can not be reassigned.
- Otherwise, if you declare a variable using var keyword, that variable will be mutable, the value of the variable can be reassigned later.
3. Variable naming
Reading your code can be pretty hard especially if your variables’ name aren’t clear enough for you to easily understand what the purpose each variable is. It’s important for you to know how to make a good variable name, let me show you how :
- Variable name must describe the information that the variable represent. Make it as clear as possible of what value the variable holds.
- Prioritize readability over concisenes of your variable name.
- Use a standard variable naming convention and be consistent throughout your entire code.
The most common practice of variable naming convention is to name your variables in camelCase. camelCase is the way to write phrases without spaces or punctuation, and for multiple word phrases we separate them by capitalizing the first character of each word (e.g. myName, password, userId, thisCamelCaseLooksCool).
— 🎁Bonus round : Bob, Joe, and aging —
Now, we are going to take a look how we use these variables to our advantage. Let’s say we have a little story about an old man named Bob.
If we want to change the character’s name, instead of Bob, we would want Joe as the name. And instead of 68 years old, we want the character be a little bit younger than that, 67. Without variables, we would have to change it manually from each of every lines.
And that’s really exhausting. Imagine if we have 1000 lines of code that contains the name and age, we would have to change them all manually! Now, Let’s see what if we use variables to store the name and age values.
We can acces the variable value for printing purpose using string literal (to get name variable value, we use $name and for age value we use $age) like the above. Now whenever we want to change the name and age, we would just have to change the value of the variables.
This variables can also assigned (for example) from user’s input so you can set the name and age when the program runs. Pretty neat right?
Don’t worry if you get confused about scanner stuff, we are going to talk about that later in this series. Try to run that code on your IDE and see what it does.
Hooray! We did it! We conquer the concept of variable declaration, mutabality, variable naming convention, and also play a little bit with Bob and Joe’s story. In the next chapter we are going to talk about data types. Ever wondered what Int and String actually is? We are going to talk about that and a bunch of other cool stuff.
Thank you so much for reading. If you like these articles, let me know in the comment section below. You can also give suggestion, I’m open for any kind of feedback.