Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




KOTLIN - VARIABLES


Variables in Kotlin are storage location where the data(Be it a number or a Name) is stored.It is like a container where something is stored.


Example :

Let us go back to our school days. You are in the Maths class and your Maths teacher had asked you to add two numbers. i.e. She asked you to add the numbers 5 and 6 and tell her the result.

And you have written the numbers 5 and 6 in your notebook, added them and and told the result 11 to your teacher.

Well! Pyton takes the same step to add two numbers. The only difference is, you have used one notebook to write and add the numbers.


But Kotlin uses different locations to store each number. And they are called as 'Variables'.


Let us see how 'Variables' are used in Kotlin for addition of two numbers.


Kotlin Code to add two numbers using 'var' keyword


Example :



fun main(){

    var x = 5
    var y = 6
    var z = x + y
    println("The added value is : "+z) 
}


Output :



 The added value is : 11

Now, if you look at the above code, 'x', 'y' and 'z' are 'Variables'.


In kotlin 'var' is the short form of 'Variables'.


That is why, we have placed 'var' before the 'Variables', 'x', 'y' and 'z'.


And below are the steps involved in addition of the numbers.

  1. 'x' is holding the first number '5'.

    var x = 5


    The above line says, create an empty location named 'x'. And assign it with the value '5'. And 'x' is called as 'variable'.

    java_Collections


    Just note that the '=' in the above code is called 'assignment operator'. It actually means the value '5' at the right should be placed in the storage location/variable 'x'.
  2. 'y' is holding the second number '6'.

    var y = 6


    Again, the above line says, create an empty location/variable named 'y'. And assign it with the value '6'.

    java_Collections
  3. And 'z' is holding the added value of (5 + 6).

    var z = x + y


    Similarly, the above line says, create an empty location/variable named 'z'. And store the added value to it.

    java_Collections
  4. Finally, we are printing the added value of (5 + 6) on the screen. Which is stored in 'z'.

    println("The added value is : "+z)


    The above println(...) statement has two sections.

    First section has the text inside double quotes '"'.

    "The added value is :"


    And the second section does not have double quotes. And is usually a 'variable'.

    z


    And they are separated by a plus sign '+'.

    "The added value is : "+z


    The plus sign '+' in the above statement is a concatenate operator. i.e. It joins the first and the second section to form a meaningful sentence.

    And we get the below output :

    The added value is : 11

So far we have seen, how to store a number (i.e. var x = 5) in a 'Variable'.


Now, let us see how to store a String(i.e. It could be a Name or a City) in a Variable.


Storing a String in a Variable using 'var' Keyword


Let us say, we want to store the String 'Hello World' in a Variable(Say 'x').


And the process of storing a String is same as storing a number, but with a mild difference.


Below is the Kotlin Code :


Example :



fun main(){

    var x = "Hello World"
    println(x)
}


Output :



 Hello World

So, in the above code, we have initialised the String 'Hello World' to the variable 'x'.


var x = "Hello World"

The only thing to note here is, if you are initialising a String (i.e. Hello World), it must be in Double Quotes ("") i.e. "Hello World".


java_Collections

Kotlin Code to add two numbers using 'val' keyword


So, we have added two numbers using 'var' keyword. But what is 'val'?


The 'val' keyword is almost same as 'var' keyword except the variables declared with 'val' keyword cannot be changed.


Let us see with the below example.


Example :



fun main(){

    val x = 5
    val y = 6
    val z = x + y
    println("The added value is : "+z) 
}


Output :



 The added value is : 11

Well! The output is just the same as we have seen in the example with 'var' keyword.


So, how is 'val' different from 'var'?


Let us clear with the below example :


Example :



fun main(){

    var x = 5
    println("The value of x is "+x)
    x = 6
    println("The value of x after the change is "+x)
}


Output :



 The value of x is 5
 The value of x after the change is 6

In simple words, 'x' is a variable.


var x = 5

i.e. It can change.


So, we have reinitialised 'x' with '6'.


x = 6

Well! Since, 'x' is a variable, it can be changed.


Now let us try the same example with 'val' Keyword.


Example :



fun main(){

    val x = 5
    println("The value of x is "+x)
    x = 6
    println("The value of x after the change is "+x)
}


Output :



 Val cannot be reassigned

And we got the error saying, 'Val cannot be reassigned'.


This is because the variable 'x' is declared with 'val' keyword.


val x = 5

And as we have mentioned, the variables declared with 'val' keyword once cannot be changed.


So, when we try changing it with the value '6',


x = 6

We get the error.


Note : The variables declared with 'var' keywords are mutable(i.e. The values can be changed) and the variables declared with 'val' keywords are immutable(i.e. The values cannot be changed)

Now, just think for a moment, you have initialised a number to a variable,


var x = 5

Or, a String to a variable,


var x = "Hello World"

But, don't you think, if there could be a way to tell Kotlin that a variable, say 'x', should only be able to hold a number and not a String.


i.e. The variable, 'x' would only contain a number.


var x = 5

And if you want to insert a String to the variable 'x', it would throw an error.


i.e. The statement


var x = "Hello World"

Would throw an error.


And luckily, that is possible in Kotlin.


Allowing only integer type in a variable


If you want to make the variable 'x' to contain only a number. You need to declare it in the below way,


Example :



fun main(){
	
    var x: Int = 5
    println(x)
}


Output :



 5

All we have done is, placed ':' and 'Int' keyword after the variable 'x'.


java_Collections

And if you want to initialise a String to 'x'. It would throw an error.


var x: Int = "Hello World"

Example :



fun main(){
	
    var x: Int = "Hello World"
    println(x)
}


Output :



Type mismatch: inferred type is String but Int was expected

And we get the above error, stating a String cannot be inserted in the variable 'x', as it is supposed to hold an integer only.


Similarly, if you want to make a variable hold only a String type value. You can do it in the below way.


Allowing only String type in a variable


Example :



fun main(){
	
    var x: String = "Hello World"
    println(x)
}


Output :



 Hello World

Similarly, we have placed ':' and 'String' keyword after the variable 'x'.


java_Collections

Kotlin Variable Naming Convention


Just like any other language, there is a rule to declare the 'Variables'.


Although, the 'Variables' can be of any names with some restriction.


Below are the rules :

  1. A lower case Variable is different from an upper case Variable.

    i.e. The lower case variable 'a' and upper case variable 'A' are different.
  2. A Variable should never start with a number.

    i.e. The Variable with name '2y' is never allowed.
  3. A Variable can contain alphabets (i.e. A to Z or a to z), numbers (i.e. 0 to 9) or an underscore (i.e. _).

So, if you have two numbers and need to assign them to two 'Variables'. You can either declare the variables as 'x' and 'y'.


x = 5

And


y = 7

But the preferred way to name the 'Variables' would be 'firstNumber' and 'secondNumber'.


firstNumber = 5

And


secondNumber = 7

But you can never use 'Variable' names with spaces like 'first number' or variables with a - like 'first-number'.