Essential Guide — If you are moving from Java to Kotlin #1/2

Some of the basic syntax of Kotlin, which are useful if you are switching from Java.

Meet K Parmar
Quick Code
3 min readMay 4, 2020

--

Image by andrekheren from Pixabay

Kotlin is easier than JAVA or other languages. Although JAVA language is used by Android developers, Kotlin is gaining popularity in the Android Community. So, this article is used as an essential guide moving to Kotlin from Java language.

In this article, we will cover the following major topics of Kotlin:

  1. Variables
  2. If statement
  3. when expression
  4. for loop
  5. while and do-while loop
  6. Nullable values and null check
  7. function
  8. class
  9. constructor
  10. Inheritance

1. Variables

Creating a variable is very easy. There are two keywords for creating a variable val and var.

val — Assigned a value only once

var — Assigned a value multiple times

Variable in Kotlin

2. If statement

In Kotlin, if statement is similar to JAVA. Check it out…

If Statement in Kotlin

Note: There is no use of Ternary operator(like, condition ? value1 : value2) in Kotlin.

Instead of the ternary operator, Kotlin use following syntax:

Ternary like operation in Kotlin

In Kotlin, if can return a value as well (we have seen in the above point). So, it can be used in the return statement of a function, too.

If statement with returning value in Kotlin

3. When

when expression is used as instead of switch case (in Java). when expression can be used to return a value, similar like if expression.

When expression in Kotlin

In Kotlin, when expression has Super Power. Let’s see this:

When with Super Power in Kotlin

Let’s take some value for input and check out the output:

1. “Hi” — “Input is a String”

2. ‘M’ — “Input is between A to Z”

3. 6 — “Input is between 1 to 10”

4. For loop

There are multiple representations or use-cases of for loop. I have covered primary use-cases here. Let’s see some of the most popular use-cases:

1. in — Simplest for loop which iterates over every element of the list

2. .. — Iterate over a range

3. withIndex() — Iterate over the list with an index of the current item.

FYI: If you want to know all the possible syntax of for loop in kotlin, then you should look in this article:

5. While and do-while loop

As we know, while and do-while loops have similar use-cases like for loop, to loop over something or some condition.

while loop first check the condition then execute the statements/block.

While loop in Kotlin

do-while loop first executes the block then check condition.

Do-While loop in Kotlin

6. Nullable values and null check

This is a very important concept for Android Developers if you are switching from Java to Kotlin.

In Kotlin, a reference must be explicitly marked as nullable when null value is possible. ? is used to indicate the possible nullable reference or variable.

7. Function

In Kotlin, the syntax of function/method is a little bit different from Java language.

  1. Following example having function with two Int parameters and Int return type:
Function in Kotlin (1)

2. function with an expression body and inferred return type:

Function in Kotlin (2)

3. function returning no meaningful value:

Function in Kotlin (3)

4. In the above case, Unit return type can be omitted, too:

Function in Kotlin (4)

8. Class

In Kotlin, classes are declared using the class keyword.

The class declaration consists of the class name, the class header, and the class body, surrounded by curly braces. Both the header and the body are optional; if the class has no body, curly braces can be omitted.

Class in Kotlin

9. Constructors

In Kotlin, constructors are declared using the constructor keyword.

The primary constructor is part of the class header: it goes after the class name (and optional type parameters).

Constructor in Kotlin

10. Inheritance

All classes in Kotlin have a common superclass Any, that is the default superclass for a class with no supertypes declared.

By default, Kotlin classes are final, they can’t be inherited. To make a class inheritable, mark it with the open keyword. To declare an explicit supertype, place the type after a colon in the class header.

Inheritance in Kotlin

I hope, this article will helpful for you. Thanks for reading!

About the Author

Hi There, I am Meet K Parmar. I am a final year student, pursuing Computer Engineering. I write stories on Tech and Programming. I love contributing to open source projects.

Connect me: LinkedInGitHubMedium

--

--

Meet K Parmar
Quick Code

Write Stories on Tech and Programming languages || Student || Self-Learner || Open Source Contributor || meetkumar.k.parmar@gmail.com