Android Kotlin-Variables, Data Types And If-Else, When Statements - Part Two

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:

  1. var name = ‘Kishor”;  
  2. 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,

  1. var name: String = ‘Kishor”  
  2. var age: Int = 23   
  3.   
  4. /** 
  5.  * Variables 
  6.  */  
  7. var name: String;  
  8. var name1: String = "Kishor";  //Initialize  
  9.   
  10. 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 

  1. /** 
  2.  * Constants 
  3.  */  
  4.   
  5. val constVariable : Int = 2  
  6. 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,

  1. var name = "Kishor"  
  2. 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, 

  1. /** 
  2.  * Arrays 
  3.  */  
  4. var arr = arrayOf("Apple""Mango", 2, 4.6) //array of different data types  
  5. var arr1: Array<String> = arrayOf("Apple""Mango""Orange")  //array of string data type  
  6. 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.

  1. /** 
  2.  * Number 
  3.  */  
  4.   
  5. val a: Int = 5  
  6. val b: Float = 5.00f  
  7. val c: Double = 5.00  
  8. val d: Long = 5000000000  
  9. val e: Short = 50  
  10. val f: Byte = 1  
  11.   
  12. println("Integer " + a);  
  13. println("Float " + b);  
  14. println("Double " + c);  
  15. println("Long " + d);  
  16. println("Short " + d);  
  17. println("Byte " + f);    
TypeBit width
Byte8
Short16
Int32
Long64
Float32
Double64

Byte

The Byte data type ranges from -128 to 127. They are 8-bit signed Two’s complement integers. 

For example,

  1. val a: Byte = 103  
  2. println("$a"//Output: 103    
Short

The Short data type ranges from -32768 to 32767. They are 16-bit signed 2’s complement integer. 

For example,
  1. val a: Short = -11103  
  2. println("$a"//Output: -11103    
Int

The Int data type ranges from -2^31 to 2^31 - 1. They are 32-bit signed 2’s complement integer. 

For example,
  1. val a: Int = 500000  
  2. println("$a"//Output: 500000    
Long

The Long data type ranges from -2^63 to 2^63 – 1. They are 64-bit signed 2’s complement integer. 

For example,
  1. val a: Long = 9900000  
  2. println("$a"//Output: 9900000  
Double

The Double data type are double precision 64-bit floating point. 

For example,
  1. val a  = 11.52  
  2. println("$a"//Output: 11.52    
Float

The Float data type is single precision 32-bit floating point. 

For example,
  1. val a  = 11.5F  
  2. println("$a"//Output: 11.5  
Characters

Characters are the single letters and are represented by Char in Kotlin. They should be declared using a single quote like ‘k’. 

For example, 
  1. /** 
  2.  * Characters 
  3.  */  
  4. val address: Char  
  5. address = 'K'  
  6. println(address)  
  7. println("$address")  //we can also print like this  
Boolean

Simply Booleans are true and false like other programming languages. 

For example,
  1. /** 
  2.  *Boolean 
  3.  */  
  4. val isPresent: Boolean  
  5. isPresent = false  
  6. println(isPresent)  
  7. println("$isPresent")  //we can also print like this  
Strings

Strings are the array of characters in,
  1. /** 
  2.  *Strings 
  3.  */  
  4. val myName: String  
  5. myName = "My name is Kishor."  
  6. println(myName)  
  7. println("$myName")  //we can also print like this  
Collections

Collections are the pile of something, such as piles of strings, integers. 

For example,
  1. /** 
  2.  * Collections 
  3.  */  
  4. val fruits = listOf("Apple""Banana""Grapes")  
  5.   
  6. val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8)  
  7.   
  8. val info = mapOf(  
  9.         Pair("name""kishor"),  
  10.         Pair("age""24"))  
  11.   
  12.   
  13. println("First fruit = " + fruits.first())  //Outputs Apple  
  14. println("Last fruit = " + fruits.last())   //Outputs Grapes  
  15.   
  16. println("List of even numbers = " +  
  17.         numbers.filter { it % 2 == 0 })      // Outputs [2, 4, 6, 8]  
  18.   
  19. println("List of odd numbers =  " +  
  20.         numbers.filter { it % 2 == 1 })    //Outputs [1, 3, 5, 7]  
  21.   
  22. println("Name = " + info["name"])     //Outputs kishor  
  23. println("Age = " + info["age"])    //Outputs 24  

In logcat, when we run the application we can see output as,

Kotlin

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.

  1. /** 
  2.  * If else 
  3.  */  
  4.   
  5. var a = 5  
  6. var b = 7  
  7.   
  8. if (a == b) {  
  9.     println("A and B are equal")  
  10. else {  
  11.     println("A and B are not equal")  
  12. }  
  13. //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,

  1. /** 
  2.  * New feature, writing expressions in if else 
  3.  */   

When the condition matches in if statement then, it returns its value and set to result,

  1. var result: Int = 0  
  2.   
  3. result = if (a < b) {  
  4.     a  
  5. else {  
  6.     b  
  7. }  
  8.   
  9. 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”. 

  1. /** 
  2.  * Switch in Java, When in Kotlin 
  3.  */  
  4.   
  5. when (a) {  
  6.     1 -> println("a is equal to 1")  
  7.     2 -> println("a is equal to 2")  
  8.     3 -> println("a is equal to 3")  
  9.     4 -> println("a is equal to 4")  
  10.     5 -> println("a is equal to 5")  
  11.     else -> println("not found")             //same like default in Java  
  12. }  
  13. //Output: a is equal to 5  
  14.   
  15. /** 
  16.  * when as expression 
  17.  */  

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,

  1. var result1: String = ""  
  2. result1 = when (a) {  
  3.     1 -> "a is equal to 1"  
  4.     2 -> "a is equal to 2"  
  5.     3 -> "a is equal to 3"  
  6.     4 -> "a is equal to 4"  
  7.     5 -> "a is equal to 5"  
  8.     else -> "not found"            //same like default in Java  
  9. }  
  10. 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,

Kotlin

Get the projects from GitHub,

  1. Variables and DataTypes
  2. If Else and When Statements

Keep you updated on Android Kotlin.

Thanks. Happy Coding.