Suppose that you are working with loops. It is sometimes desirable to terminate the loop immediately without checking the test expression. In such case, break is used. It terminates the nearest enclosing loop when encountered (without checking the test expression).
How break works?
Almost always it is used with if..else statements. For example: If expression is evaluated to true, break is executed which terminates the for loop.
Continue reading Break Expression
A function that calls itself is known as recursive function. This technique is known as recursion. A physical world example would be to place two parallel mirrors facing each other. Any object in between them would be reflected recursively.
How does recursion work in programming?
Here, the recurse() function is called from the body of recurse() function itself. Here’s how this program works.
Here, the recursive call continues forever causing infinite recursion. To avoid infinite recursion, if…else (or similar approach) can be used where one branch makes the recursive call and other doesn’t.
Continue reading Recursion and Tail Recursion
While building any application, we often need to create classes whose primary purpose is to hold data/state. These classes generally contain the same old boilerplate code in the form of getters, setters, equals(), hashcode() and toString() methods.
Kotlin has a better solution for classes that are used to hold data/state. It’s called a Data Class. A Data Class is like a regular class but with some additional functionalities.
With Kotlin’s data classes, you don’t need to write/generate all the lengthy boilerplate code yourself. The compiler automatically generates a default getter and setter for all the mutable properties, and a getter (only) for all the read-only properties of the data class. Moreover, It also derives the implementation of standard methods like equals(), hashCode() and toString() from the properties declared in the data class’s primary constructor.
Example Customer data class
Syntax of data class
Continue reading Data Class
A class is a blue print defined which groups functions and properties. Classes in Kotlin are defined using the keyword class followed by the class name. The body goes inside the curly braces.
Syntax of class in Kotlin
An instance of the class is instantiated in the following way
instance of the class
Contrary to Java, new isn’t a keyword in Kotlin.
new is not a keyword
Continue reading Class
Every Kotlin program has at least one function main(). However, you can define any number of functions in your program until memory is not full. It’s always a good practice to divide the whole program into smaller functions that are logically similar. It makes program more readable, understandable and debuggable.
A function declaration is done using the fun keyword.
The basic syntax of a function in Kotlin looks like this:
Syntax of for loop
Continue reading Functions
An Array is a collection of a values of the same type. When you need to store a list of values, such as numbers, you can store them in an array, insted of declaring separate variables for each number.
Constructor of Array in Kotlin
Syntax of Array in Kotlin
As you can see, constructor of array takes a fix size and an init function to return elements of array. There are multiple ways to create a kotlin array
- Using Library function
- Using Array Constructor
- Using Factory function
Continue reading Array of Kotlin