Kotlin Primary and Secondary Constructor

Kotlin classes can have more than one constructor. Classes can have one primary constructor and then one or more secondary constructors.   Kotlin Primary Constructor Primary constructor is in the class header and can be identified from the parameters passed. class Employee constructor(val empId:Int, var empName: String, val age:Int) If its a data class, we […]

Creating and Using Data class in Kotlin

In this article, we will see how to create and access a data class in Kotlin.   Kotlin Data class A data class’s main purpose is to hold data. We can create a data class in Kotlin using the data keyword.   Create Data Class in Kotlin To create a data class to store Employee […]

Iterating through collections in Kotlin using for loop, for with indices, for loop with range and until

In this article, we will see different ways to iterate through collections in kotlin using for loop, for with indices, for loop with range and until. Kotlin for loop Kotlin’s for loop syntax is similar to for each syntax in Java. Here are some examples. Kotlin for loop Example with Strings fun main(args: Array<String>) { […]

Managing null values in Kotlin using ? operator and KotlinNullPointerException

In this article, we will see how to create null values in Kotlin using ? operator and KotlinNullPointerException.   To declare a variable in Kotlin that can be null, we can’t declare it as a regular variable. For example, we we create a variable using val or var and assign it to null, we will […]

Kotlin : Evaluating multiple values using when expression and range

kotlin when expression In Kotlin, when is the equivalent of switch in Java or C. Here is an example of how to use the when expression.   Kotlin when example fun main(args: Array<String>) {     val num= 2     when (num){         1 -> println("One")         2 -> println("Two")         else -> println("Some other number")     } } Output Two   […]