Author

ronak

Browsing

Welcome Developers, In this article i am going to explain about Variables and Data type in Kotlin language. I will also show you how to declare variables and all basic data types in Kotlin. So let’s start.

Any programming language a variable is referred to a location in memory(storage area) to stored data. The type of variable defines the range of value that the variable can hold. So indicate a storage space, each variable should be given a unique identifier.

How to Declare a variable in Kotlin?

Kotlin programming language supports two kinds of variables. Immutable and mutable.

var is mutable where val is immutable. Value of var variable can be changed later but val variable value can not be changed.

What is Immutable ?

The immutable variable is petty self-explanatory, that means the value cannot be changed once the value is assigned. It is similar to the final variable in Java.

You want to declare a variable(immutable) in Kotlin. Suppose if I want to define a string then what is the syntax to define a variable or some string value within the main function. So for that, we have to use the keyword of val just like below

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val welcome = "Welcome to my blog!" // we can not reassign 
        Log.e("TAG",welcome)
    }
}

As earlier explained, we can’t change value of an immutable variable once you assigned. Let’s check below figure.

What is Mutable?

It holds Mutable reference, that means we can change the value later in the program. It is same as regular Java variable. mutable variable donated by var keyword.

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        var welcome = "Welcome to my blog!"
        welcome = "Variables and Data Type in Kotlin" // reassign the value
        Log.e("TAG",welcome)
    }
}

Type inference

Did you notice in the previous section, we declare a variable without specifying the type of variables. let’s take some example.

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // case 1
        var myNumber = 10 // type inferred as `Int`
        // case 2
        var myDecimal = 1.0;  // type inferred as `Float`
        // case 3
        var myString = "Kotlin Tutorial"     // type inferred as `String`
        Log.e("TAG",myString)
    }
}

Initially let’s take case 1 myNumber automatically becomes integer value now similarly in case 2 where is myDecimal value equal to let say 1.0 so this myDecimal variable simply becomes a floating point value.

All these case we have not specified the type of the variable, the compiler can understand the type of the variable by looking at the value. This is the beauty of Kotlin.

Lazy Initialization

Suppose you don’t want to initialize variable at the time of the declaration. Then you must have to specify the type of the variable during declaration. just like below

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        var helloWorld: String  // It is mandatory to specify the type in this case
        helloWorld = "Hello kotlin!"
        Log.e("TAG",helloWorld)
    }
}

Data Types in Kotlin

In Kotlin, same as java everything is an object means that we can call member functions and properties on any variable.

In Kotlin, everything is an object there are not primitive data types in Kotlin, I’ll explain the basic types used in Kotlin: numbers, characters, booleans, arrays, and strings.

Kotlin data types cheat sheet

That’s it, quite easy right? Help yourself and try kotlin in your application, Let me know if you have any questions.

Happy Coding!