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
-
-
-
-
- var name: String;
- var name1: String = "Kishor";
-
- 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
-
-
-
-
- val constVariable : Int = 2
- constVariable = 7
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,
-
-
-
- var arr = arrayOf("Apple", "Mango", 2, 4.6)
- var arr1: Array<String> = arrayOf("Apple", "Mango", "Orange")
- var arr2: Array<Int> = arrayOf(1, 2, 3, 4, 5)
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.
-
-
-
-
- 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")
Short
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")
Int
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")
Long
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")
Double
The Double data type are double precision 64-bit floating point.
For example,
- val a = 11.52
- println("$a")
Float
The Float data type is single precision 32-bit floating point.
For example,
- val a = 11.5F
- println("$a")
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,
-
-
-
- val address: Char
- address = 'K'
- println(address)
- println("$address")
Boolean
Simply Booleans are true and false like other programming languages.
For example,
-
-
-
- val isPresent: Boolean
- isPresent = false
- println(isPresent)
- println("$isPresent")
Strings
Strings are the array of characters in,
-
-
-
- val myName: String
- myName = "My name is Kishor."
- println(myName)
- println("$myName")
Collections
Collections are the pile of something, such as piles of strings, integers.
For example,
-
-
-
- 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())
- println("Last fruit = " + fruits.last())
-
- println("List of even numbers = " +
- numbers.filter { it % 2 == 0 })
-
- println("List of odd numbers = " +
- numbers.filter { it % 2 == 1 })
-
- println("Name = " + info["name"])
- println("Age = " + info["age"])
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.
-
-
-
-
- var a = 5
- var b = 7
-
- if (a == b) {
- println("A and B are equal")
- } else {
- println("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,
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)
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”.
-
-
-
-
- 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")
- }
-
-
-
-
-
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"
- }
- println(result1)
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,
- Variables and DataTypes
- If Else and When Statements
Keep you updated on Android Kotlin.
Thanks. Happy Coding.