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
Characters are the single letters and are represented by Char in Kotlin. They should be declared using a single quote like ‘k’.
For example,
- /**
- * Characters
- */
- val address: Char
- address = 'K'
- println(address)
- println("$address") //we can also print like this
Simply Booleans are true and false like other programming languages.
For example,
- /**
- *Boolean
- */
- val isPresent: Boolean
- isPresent = false
- println(isPresent)
- println("$isPresent") //we can also print like this
Strings are the array of characters in,
- /**
- *Strings
- */
- val myName: String
- myName = "My name is Kishor."
- println(myName)
- println("$myName") //we can also print like this
Collections are the pile of something, such as piles of strings, integers.
For example,
- /**
- * Collections
- */
- val fruits = listOf("Apple", "Banana", "Grapes")
- val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8)
- val info = mapOf(
- Pair("name", "kishor"),
- Pair("age", "24"))
- println("First fruit = " + fruits.first()) //Outputs Apple
- println("Last fruit = " + fruits.last()) //Outputs Grapes
- println("List of even numbers = " +
- numbers.filter { it % 2 == 0 }) // Outputs [2, 4, 6, 8]
- println("List of odd numbers = " +
- numbers.filter { it % 2 == 1 }) //Outputs [1, 3, 5, 7]
- println("Name = " + info["name"]) //Outputs kishor
- println("Age = " + info["age"]) //Outputs 24
In logcat, when we run the application we can see output as,

Now, let’s see the new features in if else and when statements in Kotlin. In Kotlin, we can write if else and when statements as expressions too.
If-Else
In Kotlin, if-else is similar to other programming languages, but it has one new feature, i.e., we can write expressions in if-else as shown below.
- /**
- * If else
- */
- var a = 5
- var b = 7
- if (a == b) {
- println("A and B are equal")
- } else {
- println("A and B are not equal")
- }
- //Output: A and B are not equal
This means a and b are equal. And hence it prints “A and B are not equal”.
We can use if-else as an expression in Kotlin as,
- /**
- * New feature, writing expressions in if else
- */
When the condition matches in if statement then, it returns its value and set to result,
- var result: Int = 0
- result = if (a < b) {
- a
- } else {
- b
- }
- println(result) //Outputs 5
When as a switch in Kotlin
In Kotlin, switch case is changed to when and also, we can write expressions in when which is shown below.
For example,
We pass (a = 5) then it matches with 5 and prints “a is equal to 5”.
- /**
- * Switch in Java, When in Kotlin
- */
- when (a) {
- 1 -> println("a is equal to 1")
- 2 -> println("a is equal to 2")
- 3 -> println("a is equal to 3")
- 4 -> println("a is equal to 4")
- 5 -> println("a is equal to 5")
- else -> println("not found") //same like default in Java
- }
- //Output: a is equal to 5
- /**
- * when as expression
- */
Explanation: When the condition matches in the When statement (when a = 5), then it executes the matched code block and prints “a is equal to 5”.
Similarly, we can set the result to some variables when the condition is matched.
For example,
- var result1: String = ""
- result1 = when (a) {
- 1 -> "a is equal to 1"
- 2 -> "a is equal to 2"
- 3 -> "a is equal to 3"
- 4 -> "a is equal to 4"
- 5 -> "a is equal to 5"
- else -> "not found" //same like default in Java
- }
- println(result1) //Output: a is equal to 5
Explanation: When the condition matches in when statement then, it returns its value and set to result1.
In logcat, when we run the application, we can see the output as,

Get the projects from GitHub,
Keep you updated on Android Kotlin.
Thanks. Happy Coding.

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