Variables – Declaring and Understanding WHY they are so Important!

Say you have following 4 sentences that you want as output.

Welcome to Kotlin. In this lecture we are studying about variables. We have already discussed print commands in earlier lessons. Use of variables make coding easy to write, process and maintain.

In order to output this set of 4 sentences I will write a simple code according to what we have learnt so far:

And our output will be as follows:

Easy peasy!

Now duplicate this code 4 times further with each passage printing in a new line. You will have 16 sentences and your code will be with println now:

Lost already? It has just begun. Now for every println command, move the third sentence before the second one so that it becomes like this:

Welcome to Kotlin. We have already discussed print commands in earlier lessons. In this lecture we are studying about variables. Use of variables make coding easy to write, process and maintain.

Don’t feel like trying? Daunting task! Imagine you have to do this for thousands of lines of code manually.

This is where variables help us greatly. With variables defined once, we can change the code itself at instances to suit our needs OR we change the value of variables to have its effect throughout the code effortlessly.

Note: Variables are of two types i.e. mutable (changeable i.e. in actual variables) and immutable (unchangeable i.e. in actual values). We will discuss both of them in detail later.

Now see how we achieve the task discussed above through variables.

In order to use variables, we need to first define them. We can define variables using either val or var keyword followed by the name of the variable (whatever you desire) and then usually equating it to its value as following:

Remember:

  1. val is the keyword to tell IDE we are defining a variable
  2. line1 is the name of the variable we gave
  3. “Welcome to Kotlin” is the value of variable named “line1”

As we have four sentences we need to define four variables. I did so as following:

Here is the code so far:

Now lets print this as one passage just like normal. For this we need to add following print command at the end of the above code:

Note: We call or less technically saying mention variables using dollar sign before their name so that IDE knows we are referring to some variable. As we have line1, line2, line3 and line4 as variables, IDE will fetch the values of these variables for us. Our final code will look like this:

Hit SHIFT+F10 and our output will be:

Our output is exactly same as we had in our first step at the start of this post.

Now the requirement of having the same passage printed 4 times with third line before second. Remember? This is where variables will do its magic.

Copy and paste the println command three times so that we have a set of four as following:

Now we have to options to achieve the requirement of having third sentence before the second one:

Option 1: Change the values of line2 and line 3 variable with each other by cutting and pasting.

Option 2: Simply change the name of variables to make line2 as line3 and line 3 as line2.

I think Option 2 is better! Watch it happen in action:

See how such a painful task became super painless only because of variables. This is how variables help us perform different operations with different instructions implemented on and around them in the code to make one easily maintainable program and ultimately an app!

We will discuss more on variables next!

Leave a Reply

Your email address will not be published. Required fields are marked *