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.
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.
fun main(){ var x = 5 var y = 6 var z = x + y println("The added value is : "+z) }
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.
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.
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 :
fun main(){ var x = "Hello World" println(x) }
So, in the above code, we have initialised the String 'Hello World' to the variable 'x'.
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".
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.
fun main(){ val x = 5 val y = 6 val z = x + y println("The added value is : "+z) }
Well! The output is just the same as we have seen in the example with 'var' keyword.
Let us clear with the below 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) }
In simple words, 'x' is a variable.
i.e. It can change.
So, we have reinitialised 'x' with '6'.
Well! Since, 'x' is a variable, it can be changed.
Now let us try the same example with 'val' Keyword.
fun main(){ val x = 5 println("The value of x is "+x) x = 6 println("The value of x after the change is "+x) }
And we got the error saying, 'Val cannot be reassigned'.
This is because the variable 'x' is declared with 'val' keyword.
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',
We get the error.
Now, just think for a moment, you have initialised a number to a variable,
Or, a String to a variable,
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.
And if you want to insert a String to the variable 'x', it would throw an error.
i.e. The statement
Would throw an error.
And luckily, that is possible in Kotlin.
If you want to make the variable 'x' to contain only a number. You need to declare it in the below way,
fun main(){ var x: Int = 5 println(x) }
All we have done is, placed ':' and 'Int' keyword after the variable 'x'.
And if you want to initialise a String to 'x'. It would throw an error.
fun main(){ var x: Int = "Hello World" println(x) }
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.
fun main(){ var x: String = "Hello World" println(x) }
Similarly, we have placed ':' and 'String' keyword after the variable 'x'.
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 :
So, if you have two numbers and need to assign them to two 'Variables'. You can either declare the variables as 'x' and 'y'.
And
But the preferred way to name the 'Variables' would be 'firstNumber' and 'secondNumber'.
And
But you can never use 'Variable' names with spaces like 'first number' or variables with a - like 'first-number'.