In my previous article, we executed code snippets in Kotlin without the need for a class. It worked well for small code snippets, but that isn’t an excellent option for production-ready applications. Let’s look into how to create a class in Kotlin.
I don’t need to create a new file for this purpose. Hence I will update the same HelloWorld.kt.
In Kotlin, we can initialize a class without the new() keyword (line number 5 & 8). Additionally, providing a class type while initializing is not mandatory, as you can see in the sample above.
Immutability: var vs val
Kotlin comes with specific keywords for mutable and immutable behaviors.
- ‘var’ keyword means that the variable is mutable. i.e., the value can change.
- ‘val’ keyword indicates that the variable is immutable, i.e., the value can’t change.
Let’s try and understand this with an example.
There is a compilation error in the sample above. The variable calculator2 is immutable, but I am trying to recreate the instance. The compiler does not allow this, as can be seen in the line highlighted in Red.
I will take another example where the class is mutable, but one of the member variables is not. There is a compilation error in the line highlighted in Red.
String interpolation is a way of replacing values for variables in string operations. Curly braces help in achieving string interpolation. Let’s look at the various styles of doing it.
An important note here is that println, automatically converts an integer (i.e., result) to string (line number 17) and performs string concatenation. The same is true for the object on line number 18.
The output of this class will be:
As with any other programming language, Kotlin supports branching with the help of if-else statements. Kotlin does not support ternary operators as in Java. Instead, if-else statements in Kotlin return values. See the samples below:
- Line number 16–23 shows a basic if-else statement.
- Line number 26–32 shows an if-else statement that returns a value i.e., its used as an expression.
Kotlin does not support Switch keyword. Instead, there is a When keyword to achieve the same objective.
I have created a function Calculate() in the class Calculator to demonstrate the working of when keyword. The function calculate() performs a mathematical operation on two numbers based on the operation name passed as input. See sample code below:
A beneficial feature of IntelliJ IDEA is that we can replace “when” code block with “if-else” and vice versa with a couple of clicks:
Try Catch expression
Similar to if-else, try-catch block can also be used as expressions i.e. it can be used to return value. Let’s demonstrate this with an exception caused by dividing a number by zero.
In this sample, I want to return the result of the division operation to be -1 if a number is divided by 0.
The output of this snippet will be -1 as I am dividing a number by zero.
While and Do While Loops
While and Do-While loops are exactly the same as Java but I will share a code snippet for clarity.
Output of this code snippet is:
There are various variations of for loops in Kotlin, and some of them are very powerful. However, I will limit this article to primary usage.
I will be covering more samples after I have covered collections.
The output of code snippet is
There is a lot more to cover and I am learning as I am writing about Kotlin