when to use val and var in kotlin, What is the difference between var and val in kotlin, var in kotlin, val in kotlin, datatypes in kotlin, kotlin example

What is the difference between var and val in kotlin

As we all know, every programing language has different data types that store or hold different types of data.

There is a need to learn each type of data type in different programming languages so that you can understand the meaning of each and avoid contradicting it with how it is used in another language.

While developing android applications, java language has been the most common language that has been used since the launch of android development by google.

Over time, kotlin has also come in place and is used together with java to develop android applications through with time, kotlin will be the official language that will be used as the main language to develop android applications

While using java to develop android applications, there have been different data types that have been used which include integer written as int and string written as a string.

In kotlin, there has been a change in how we assign values and we no longer use strings or integers.

The datatypes that are mostly used in kotlin are var and val.

In this article, we shall focus to understand what var and val means, what are the differences between var and val in kotlin, and also show how each is used in kotlin and the type of data each holds.

What is var and Val in Kotlin

 Var in kotlin represents variable meaning that the data or value stored using this datatype can change or vary from time to time

Val in kotlin represents value meaning that the data stored using this data type cannot change.

What is the difference between var and val in kotlin

The biggest difference between var and val in kotlin is that var represents a variable that can be reassigned to another value meaning the data stored using the var attribute can change from time to time but val represents a value that when defined and data assigned to it, it cannot change.

A val datatype is the same as constant in other languages including java.

When a val is used to define data, the value of that object becomes read-only meaning it cannot be changed.

Examples demonstrating how var and val are different  

Example of using var

In this example, we have the current hour as 12 which is assigned to data type var but after 1 hour, we want to reassign a different value to the same variable of the next hour

import java.util.*

 

fun main(args: Array<String>) {

    var current_hour = 11

    print("The current hour is : "+current_hour)

 

    // after 1 hour

    current_hour = 12

    print("\The current hour after one hour is : "+current_hour)

   

}

The result for the above code will be

The current hour is : 11

The current hour after one hour is : 12

The same variable current_hour assigned at two different instances holds different values at a given time

Example of using val

As in the example above, we shall change the data type to val and notice the result

import java.util.*

 

fun main(args: Array<String>) {

    val current_hour = 11

    print("The current hour is : "+current_hour)

 

    // after 1 hour

    current_hour = 12

    print("\The current hour after one hour is : "+current_hour)

   

}

The result of the above example using val is as below

prog.kt:8:5: error: val cannot be reassigned

    current_hour = 12

The result clearly shows that you cannot reassign the value defined using val datatype

Conclusion

As of the above discussion, you can clearly understand the difference between val and var data types in kotlin. As you work with each, ensure you know clearly what you want to achieve while you use any of the data types.