Kotlin Fundamental

Variables and Data Type in Kotlin

Pinterest LinkedIn Tumblr

Welcome Guys, In this article, I’m going to explain the variables and data type in Kotlin. I also explain how to declare variables and all basic data types in Kotlin. So let’s get started.

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.

Declare a variable in Kotlin?

Kotlin programming language supports two kinds of variables.

  • Immutable
  • Mutable
variable in kotlin example
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

fun main(args: Array<String>) {

    val welcome = "Welcome to Kotlin Tutorial" // we can not reassign 
    print(welcome)

}

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

how to declare a variable

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.

fun main(args: Array<String>) {

    var welcome = "Welcome to Kotlin Tutorial"
    welcome = "Variables and Data Type in Kotlin" // reassign the value
    print(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.

fun main(args: Array<String>) {
    // 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`


    print(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

fun main(args: Array<String>) {
    
    var helloWorld: String  // It is mandatory to specify the type in this case

    helloWorld = "Hello World "

    print(helloWorld)

}

Summery

variable and data types in kotlin

Data Types in Kotlin

In Kotlin, sam 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

data types in kotlin

Read more about range read Kotlin’s official documentation. In this tutorial, we have learned the type of variable and data type in Kotlin. Still, you have any queries let me know.

Read our recent post on Why TypeScript is better than JavaScript

Write A Comment