Kotlin

Variable Declaration in Kotlin

In this article, I will demonstrate several examples of the Variable Declaration in Kotlin. As a matter of fact, Kotlin was developed for Java Virtual Machine (JVM). Therefore Java and Kotlin are interoperable. Further, Kotlin has been used for Android app development.

In general, Kotlin creates variables in two ways – using the val, and var keywords. While variables declared with val keyword are immutable. However, variables created with var keywords are mutable. Furthermore, in the case of Kotlin, specifying data type during variable declaration is not mandatory. Rather, Kotlin infers the data type from the value assigned to the variable.

In fact, the const keyword also makes a variable constant and immutable. However, the difference between const and val is that const creates a constant at compile-time.

Examples of Variable Declaration in Kotlin

As shown below, variables declared with val can’t be modified. While the ones declared using the var keyword can be changed in the program.

fun main(args: Array<String>) {
    val a=100;
    var b=200;
   // ++a;
    ++b;

    println("Value of a and b is $a and $b respectively ");
}

Moreover, the keyword const is not applicable to local variables. Hence, we create const variables outside functions. The following code demonstrates it.

const val x = 1000;
fun main(args: Array<String>) {
   //const val y=10;

   println("Value of x is $x");
}

Data Types in Kotlin

Basically, the numeric data types are categorized as Integer data types and floating-point data types. Further, there are four integer types – Byte, Short, Int, Long. Meanwhile, the two floating-point data types are Float and Double. The following code shows examples of variable declarations using numeric types.

fun main(args: Array<String>) {
   var a : Byte =100;
   var b : Int = 500;
   var c : Int;
   c=a+b;

   println("Sum of $a and $b is $c");
  // a=800; It will result in compile error
   a=800.toByte();
   b=1200;
   c=a+b;
   println("Sum of $a and $b is $c");
}

Output

Demonstrating Variable Declaration in Kotlin
Demonstrating Variable Declaration in Kotlin

As shown above, the values assigned to the variables a, and b in first part of the program fall within the range of their respective data types. Hence, the result produced is correct. However, in the second part the value 800 exceeds the range of Byte variables. Therefore, it is truncated. Accordingly, the result is produced with the truncated value of a. Moreover, an explicit cast is required to convert the integer 800 into a Byte value.

Apart from numeric data types, Kotlin has Boolean and Char data types. While Boolean types can take the value of either true or false. Similarly, a Char type variable can accept a single character. In contrast to Java, the Char and Int types are not equivalent in Kotlin. The following code shows examples of Boolean and Char type variable declarations.

fun main(args: Array<String>) {
   var a : Boolean =true;
   var b : Char = '@';

   println("The Two Values are:  $a and $b");
 }

Output

Boolean and Char Variable Declaration in Kotlin
Boolean and Char Variable Declaration in Kotlin

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *