In this article, we will learn variables, constants, and data types as well as If-Else and When statements in Kotlin.
Please have a look at my previous article before continuing with this one.
Variables
In Kotlin, we declare variables using keyword var or val. For example:
- var name = ‘Kishor”;
- var age = 23 ;
Here, name is variable of string type, and age is variable of integer type. This means it is not necessary to specify the type of variables in Kotlin. The compiler will automatically know if it is a String or an Integer.
Having said that, we can specify its type if we want. Like,
- var name: String = ‘Kishor”
- var age: Int = 23
- /**
- * Variables
- */
- var name: String;
- var name1: String = "Kishor"; //Initialize
- println("My name is " + name1);
It gives output as: My name is Kishor
Constants
Constant variables are those value of which cannot be changed once initialized. In Kotlin, to define a constant variable, we use val
- /**
- * Constants
- */
- val constVariable : Int = 2
- constVariable = 7 //Gives error, we cannot reassign new value to constant variables
Kotlin knows what type of variable you are assigning, so it is not necessary to tell the compiler what kind of variable you are defining. Thus, we can define our variables as,
- var name = "Kishor"
- val constVariable = 2
Notice that, we don’t need to put a semicolon at the end of the line.
Arrays
Arrays are the collections of a single type of data. For example, we can create an array that can save 1000 values of String type.
In Kotlin, we define an array as,
- /**
- * Arrays
- */
- var arr = arrayOf("Apple", "Mango", 2, 4.6) //array of different data types
- var arr1: Array<String> = arrayOf("Apple", "Mango", "Orange") //array of string data type
- var arr2: Array<Int> = arrayOf(1, 2, 3, 4, 5) //array of integer data type
Some basic data types
In Kotlin, we can call a function and properties of any variable so we can call everything as an object. The number representation in Kotlin is similar to that in Java.
- /**
- * Number
- */
- val a: Int = 5
- val b: Float = 5.00f
- val c: Double = 5.00
- val d: Long = 5000000000
- val e: Short = 50
- val f: Byte = 1
- println("Integer " + a);
- println("Float " + b);
- println("Double " + c);
- println("Long " + d);
- println("Short " + d);
- println("Byte " + f);
| Type | Bit width |
| Byte | 8 |
| Short | 16 |
| Int | 32 |
| Long | 64 |
| Float | 32 |
| Double | 64 |
Byte
The Byte data type ranges from -128 to 127. They are 8-bit signed Two’s complement integers.
For example,
- val a: Byte = 103
- println("$a") //Output: 103
The Short data type ranges from -32768 to 32767. They are 16-bit signed 2’s complement integer.
For example,
- val a: Short = -11103
- println("$a") //Output: -11103
The Int data type ranges from -2^31 to 2^31 - 1. They are 32-bit signed 2’s complement integer.
For example,
- val a: Int = 500000
- println("$a") //Output: 500000
The Long data type ranges from -2^63 to 2^63 – 1. They are 64-bit signed 2’s complement integer.
For example,
- val a: Long = 9900000
- println("$a") //Output: 9900000
The Double data type are double precision 64-bit floating point.
For example,
- val a = 11.52
- println("$a") //Output: 11.52
The Float data type is single precision 32-bit floating point.
For example,
- val a = 11.5F
- println("$a") //Output: 11.5



Kartik PawarPosted Mar 20, 2018, 1:58 PM
Nice article for learning Kotlin. Keep sharing...
Sagar Pandurang KapPosted Mar 14, 2018, 11:36 PM
Thanks for the nice article