Kotlin Variables and Data Types



Variables

  • Variable is a box where you store your data. It has a name by which you access this data or update this data. Let's take one example - you are playing a game where you have your score. Every time you do something in the game - your score increments. 
  • This score is stored as a variable in your application. So simply put - a variable is a place to store data that changes when your application runs. That's why it is named as a variable - something that varies. 
what-is-a-variable
  • In Kotlin, there are 2 types of variables - var and val. The difference between var and val is - if you want to re-assign values to a variable then you define it using var and if you don't want to re-assign value define it as val.
       var score = 5 
       val playerName = "John"
  • The above snippet shows two variables - score which is defined using var because we want to keep updating the score as the game progresses whereas the playerName variable is defined using val because the player name remains the same for the entire game.
val can't be re-assigned

var can be re-assigned 


Data Types In Kotlin

  • As said above, variables are simple boxes. Now we need to define the size of the box and we need to define the type of data you will be storing in these boxes.
  • For this information, we have data types - Data Types define the type of the data you will be storing in a variable and also defines the size.
  • Kotlin has a variety of data types to store text data, numbers, booleans. These are as follows -

    Numbers - 

  • INTEGER (Byte, Short, Int, Long) - Numbers without decimals. E.g. 1, -5, 10, 224545
  • FLOATING POINT ( Float, Double) - Numbers with decimals. E.g. 11.23, 3.14 etc

    Text Data -

  • Char - Stores single character E.g. M or F
  • String - Store multiple characters. E.g. Hello, CheezyCode, Kotlin, etc. 

    Boolean

  • True/False 

Based on the value, you decide which data type is needed. For e.g. if you want to store a number that does not have decimal values in it - we will choose either Byte or Short or Int or Long. Now based on the size of the value we will choose one. Refer to the below image - 


Similarly, if we have decimal values - we will use floating points i.e. either float or double. 


Let's take some examples - 
    	val name = "John" // String
        val gender = 'M' // Character        
        var perimeter = 30 // Integer        
        val PI = 3.1415926535 // Double - Floating Point        
        var message = "Welcome To CheezyCode" // String        
        val isRaining = false // Boolean
    
This is how we define variables in Kotlin. 

Type Inferencing

  • Kotlin has this feature of identifying the data type based on the value you assign to a variable. It identifies the type automatically. This is called Type Inferencing - Kotlin infers the type on its own.
  • So let's say you assign a string value to a variable then we don't need to specify the type of a variable that - Hey Kotlin! This variable is String. Kotlin is smart enough to judge that from the value itself.
  • Most of the times this feature really helps but sometimes we need to explicitly declare the type of a variable - in those cases, we explicitly define the type of the variable using the below syntax - 
      val i: Int = 2
      val msg : String = "Game Over"      
      val isVisible: Boolean = true
    
That's it for this article. For more articles on Kotlin Tutorials For Beginner Series - refer to this.

Happy Learning !! Cheers From CheezyCode.


Comments

Popular posts from this blog

Just Maths - Views, View Groups and Layouts

Create Android Apps - Getting Started