Home » Blog » Kotlin Basics – Kotlin Variables

Kotlin Basics – Kotlin Variables

There are two types of variables – mutable and immutable. An immutable variable is one whose value cannot be changed, also known as unchangeable or read-only variable. On the other hand the value of the mutable variable can be changed.

Immutable variable: val keyword

Immutable variable is declared using val keyword in Kotlin. In this example, we have declared an immutable variable myName using val keyword and later displayed the value of it.

package tejprogramming

fun main(args : Array<String>){
    /**
     * This is an immutable variable
     * also called unchangeable variable
     * or read-only variable.
     */
    val myName = "Suvidha"
    println("My Name is: "+myName)
}

 

What happens when we try to change the value of an immutable variable?
If we try to change the value of an immutable variable, we will get a compilation error, just like we get in the following example.

package tejprogramming

fun main(args : Array<String>){
    /**
     * This is an immutable variable
     * also called unchangeable variable
     * or read-only variable.
     */
    val myName = "Suvidha"
    myName="Suvidha Malaviya"
    println("My Name is: "+myName)
}

Output:

Error:(13, 5) Kotlin: Val cannot be reassigned

Mutable variable: var keyword

Unlike immutable variable, we can change the value of a mutable variable. In kotlin, we use var keyword to declare a mutable variable. Lets take an example to understand this.

In this example we have declared a mutable variable using var keyword, to demonstrate that we can change the value of it, we have reassigned a different value to myName variable.

package tejprogramming

fun main(args : Array<String>){
    /**
     * This is an mutable variable
     * we can change the value of this
     * variable
     */
    var myName = "Suvidha"
    myName="Suvidha Malaviya"
    println("My Name is: "+myName)
}

 

What is a variable?

A variable is a name, given to a location in memory that can hold data. For example, when I declare the variable like this:

var website = "tejsumeru"

The data “tejsumeru” is stored in a memory at a particular location, named as website.

Here var is a keyword which is used for declaring a variable, website is an identifier(name of the variable), “tejsumeru” is the data(value of the variable) and the type of variable is String(we will discuss it later).

Type inference

As we mentioned earlier, we can declare and initialize the variable in a single statement like this:

var website = "tejsumeru"

In the above statement we have not specified the type of the variable, kotlin knows that the variable website is a string. Compiler can understand the type of the variable by looking at the values.

However if you want to explicitly mention the type of the variable then you can do that like this:

var website: String = "tejsumeru"

Here we have explicitly mentioned the type of variable “website” as String.

Declaration first and initialization later:
We can declare the variable first and then we can initialize it later in the program. The important point to note here is that we you do it like this, you must have to specify the type of the variable during declaration.

//It is mandatory to specify the type in this case
var website: String
website = "tejsumeru"

Kotlin Data Types

1. Numbers – Byte, Short, Int, Long, Float, Double
2. Boolean – True, false
3. Characters
4. Arrays
5. Strings

Numbers

We have several data types to represent numbers in Kotlin.

1. Byte

The range of Byte data type is -128 to 127. This is used for representing the smaller integer values.

fun main(args : Array) {
    val num: Byte = 99
    println("$num")
}

Output:

99

You must be aware of the range of the data type to avoid errors. For example, the following code would generate an error because the value assigned to the variable of type byte is not in the range.

fun main(args : Array) {
    //Range is -128 to 127
    val num: Byte = 300
    println("$num")
}

Output:
error

we can also know the min and max value of a data type like this:

package tejprogramming

fun main(args : Array<String>){

    var bigByte: Byte = Byte.MAX_VALUE
    var smallByte: Byte = Byte.MIN_VALUE
    println("Biggest Byte value is: " + bigByte)

    /**
     * I can use String interpolation and put
     * the variable inside quotes to display its
     * value
     */
    println("Smallest Byte value is: $smallByte")
}

2. Short

The range of Short data type is -32768 to 32767.

fun main(args : Array) {
    val num: Short = 29999
    println("$num")
}

Output:

29999

You may be wondering when we have a larger range in Short data type, why we use Byte data type?
This is to save the memory. The Short data type holds more memory compared to the Byte data type so if you are sure that the value would be in the limit of -128 to 127 then the Byte data type would be a better choice from the memory perspective

3. Int

The range of Int data type is -231 to 231-1

fun main(args : Array) {
    val num: Int = 123456
    println("$num")
}

Output:

123456

Note: If you do not explicitly specify the type of the variable then the compiler would treat that variable as Int, if the value ranges between -231 to 231-1

4. Long

The range of Long data type is -263 to 263-1

fun main(args : Array) {
    val num: Long = 12345678
    println("$num")
}

Output:

12345678

Note: If you do not explicitly specify the type of the variable then the compiler would treat that variable as Long, if the value is out of the range of -231 to 231-1 but lies in the range of -263 to 263-1

5. Double

fun main(args : Array) {
    // all floating numbers are double by default
    // unless you suffix the value with F letter
    val num = 101.99
    println("$num")
}

Output:

101.99

6. Float

fun main(args : Array) {
    // This is a float data type as we have suffixed
    // the value with letter 'F'
    val num = 101.99F
    println("$num")
}

Output:

101.99

Boolean

The value of boolean variable is either true or false.

fun main(args : Array) {
    val boolValue = false
    println("$boolValue")
}

Output:

false

Char

All english alphabets(lowercase or uppercase) are included in Char data type. You cannot assign numbers to the variable of Char data type.

fun main(args : Array) {
    val ch = 'A'
    println("$ch")

    val ch2: Char
    ch2 = 'Z'
    println("$ch2")
}

Output:

A
Z

 

Leave a Reply

Your email address will not be published.