Variables in Kotlin
Tags: kotlin, codenewbie, beginners, todayilearned
Published: 2019-07-12 17:41:41

* Variables are the names you give to computer memory locations which are used to store values in a computer program.

* They provide a way to labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves as well.

* It is helpful to think of a variable as a box that stores particular values. Their sole purpose is to label and store data in memory. So that data can be used easily throughout your program.

* There are two types of variables in kotlin:
1. Mutable variables (declared using `var` keyword)
2. Immutable variables (declared using `val` keyword)

##Variable Declaration
---
* To declare a variable in kotlin, either `var` or `val` keyword is used.

* There are only two possibilities in kotlin for variable declaration.


1. Through Type Inference (where inline initialization is compulsory)
2. Through explicit data type declaration (here you can initialize variable later)

##Syntax for declaring variable in kotlin
---
***Syntax for Declaration through type inference***
```
var VariableName = value    //Initializer is compulsory
val VariableName = value    //Initializer is compulsory
```

***Syntax for explicit data type declaration***
```
var VariableName  : data type = value    //Initializer is optional
val VariableName : data type            //Initializer is optional
VariableName = value              // initialize value at this point
```

***Example***
```
val language = “French”
var score = 40
```

* Here, language is a variable of type ***String*** and the score is a variable of type ***Int***.

* You don’t have to specify the data type of variable in kotlin.

* Compiler knows this by initializer expression like here, it knows that “French” is a ***string***, and 40 is a ***integer*** value in the above program. This is called Type Inference in programming.

* However, you can explicitly specify the data type if you want. For example.

```
var language: String = “French”
val score: Int = 40
```

* As you can notice, in kotlin we first write the variable name and then put a colon and then specify the data type of that variable.

* We have initialized variable during declaration in above examples.

* However, it is not necessary, you can declare the variable and specify its type in one statement and initialize the variable in another statement later in the program.
```
var language : String 
language = “Marathi”

val score         //Compile time Error
Score = 60        //Compile time Error
```

* In the last statement, the type of language variable is not explicitly specified, nor the variable is initialized during declaration which can cause a compile-time error. You have to choose one of them either initialize variable during declaration or explicitly specify its data type.

So, guys, that’s it for variables in kotlin. This is pretty much in-depth detail about variables. That’s it for today guys. Please check every day for more articles.

Till then Keep Coding, Keep Loving.

Jai Hind, Vande Mataram 🇮🇳

Wanna get in touch with me? Here are links. I’ll love to become your friend. 😊

- [My Site] (https://www.jaytillu.in)
- [My Blogs] (https://blogs.jaytillu.in)
- [Twitter](https://twitter.com/jay_tillu)
- [Facebook](https://www.facebook.com/jaytillu.1314/)
- [Instagram](https://www.facebook.com/jaytillu.1314/)
- [Medium](https://medium.com/jay-tillu)
- [LinkedIn](https://www.linkedin.com/in/jaytillu/)
- [Github](https://www.github.com/jay-tillu)
- [Stack overflow](https://stackoverflow.com/users/8509590/jay-tillu)
or just mail me at jd.tillu@gmail.com