Category Archives: Kotlin tutorials

Kotlin tutorials category is made for beginners and for experts as well. To start learning kotlin this tutorial could be a good start.
But first let’s talk a little bit about kotlin.

What is Kotlin all about?

It is a new programming language targeting the Java platform. Kotlin is concise, safe, pragmatic, and focused on interoperability with Java code. It can be used almost everywhere Java is used today.

  • For server-side development
  • Android apps
  • JavaScript
  • Native
    • Kotlin/Native is a technology for compiling Kotlin code to native binaries, which can run without a virtual machine.

Kotlin works great with all existing Java libraries and framworks and runs with the same level of performance as Java.

Target platforms: Server-side, Android, anywhere Java runs

The primary goal of Kotlin is to provide a more concise, more productive, safer alternative to Java that’s suitable in all contexts where Java is used today. Java is an extremly popular language, and it is used in a broad variety of environments, from smart cards to the largest data centers run by Google, Twitter, LinkedIn, and other internet-scale companies. In most of these places, using Kotlin can help developers achieve their goals with less code and fewer annoyances along the way.

The most common areas to use Kotlin are

  • Building server-side code (typically, backends of web applications)
  • Building mobile applications that run on Android devices

In addition to Java, Kotlin can be compiled to JavaScript, allowing you to run Kotlin code in the browser.

Break Expression

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

Follow and like us:

Recursion and Tail Recursion

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.


Example

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

Follow and like us:

Continue Expression

Suppose you are working with loops. It is sometimes desirable to skip the current iteration of the loop. In such case, continue is used. The continue construct skips the current iteration of the enclosing loop, and the control of the program jumps to the end of the loop body.

How continue works?

Almost always it is used with if..else statements. For example: If the expression is evaluated to true, continue is executed which skips all the codes inside while loop after it for that iteration.

Continue reading Continue Expression

Follow and like us:

Data Class

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

Follow and like us:

Getters and Setters

Getters and Setters are used to effectively protect your data, particularly when creating classes. For each instance variable, a getter method returns its value while a setter method sets or updates its value. Getters and setters are also known as accessors and mutators, respectively.

Accessing properties directly is not a good practice.

Sometimes we need to check the value in case of an invalid input, or do some other data validation or calculation before assigning or accessing the property value.

Continue reading Getters and Setters

Follow and like us: