Kotlin Fundamentals in Examples

Artem Diashkin
Mar 24 · 6 min read

In this article we will take a look at the fundamental features of a Kotlin language “A modern programming languagethat makes developers happier”.

What will be covered in this article:

  • No “public static void main”
  • Creating an instance
  • High-order functions and lambdas
  • If-else
  • Null safety
  • No switch statement/When expression
  • Try/catch/finally
  • Ranges

No “public static void main”

Let’s begin our journey to Kotlin by creating a simple example of a required function in Java (the entry point of Java’s applications):

public static void main(...){}

How it looks in Java:

Hot it looks in Kotlin

You might notice few differences:

  • we didn’t declare a class Kotlin will do that for us by wrapping our functions with a class with our file's name. Filename → Example.kt ; the class name will be → ExampleKt. We could override it by using @JvmName annotation at the top of our file : @file:JvmName(“ClassCustomName”)
  • we used fun to declare a function, and we didn’t add public static void, Kotlin will do that for us;
  • we used println() instead of System.out.println()

⚠️NOTE: You can use Tools -> Kotlin -> Show Kotlin Bytecode option in IntelliJ IDEA if you want to see the Decompiled version of a Kotlin code:

Let’s decompile our Kotlin example and see the results:

⚠️ NOTE: If you want to run compiled <name>.kt.class file, you will need to use kotlin-runtime.jar file if you prefer using CLI

Creating an instance

Now, let’s take a look at how we can declare a class and create instances. We will create a class in Java and in Kotlin with the same signature, but later, you will see that the result is totally different:

Creating an instance in main function:

You might notice few differences:

  • we didn’t use new keyword in Kotlin;
  • we didn’t specify user’s type, just declared a value ;
  • we’re accessing name like it was declared as public, but actually, it is not;

Decompiled result:

As you can see, Kotlin has done a lot of stuff for us.

  • added private to the name field, class fields are private by default;
  • classes are public by default;
  • added getter;
  • added setter;
  • added checks for Null;

Decompiled main:

And it added new for us, cool 😉

High-order functions and lambdas

If you are familiar with JavaScript, you should be familiar with the callback functions.

In Kotlin, a higher-order function is a function that takes functions as parameters or returns a function. And lambda expressions are like an anonymous function.

Let’s take a look at a simple example of using a higher-order function without using a lambda:

Result:

Hello world!!!

But how lambda works? We will modify show function invocation:

Nice 🙂

If you want to know more about this topic, you can check here:

If-else

In Kotlin, if-else is not just a statement. It can be used as an expression. It means it can return values.

Let’s take a closer look:

Result

Result is 42

Null safety

If we declare a val use = User() Kotlin will add User type under the hood:

val user: User = User()

It means that variable cannot be Null. Kotlin will throw an error if it is.

But what if we want to allow user to be Null ? In that case, you can tell Kotlin about it by adding a ? sign at the end of a type:

val user: User? = User() // now "user" can be NULL

But let's change that type from User to User? . What will happen?

Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type User?

After that, we will need to modify our code where we are trying to access the name field.

Hello TypeScript 🙂

If we run our code result would be:

User's name is null

If you want to know more about this topic, you can check here:

No switch statement/When expression

In Kotlin, instead of a switch/case -> when/else is used, and it is not a statement (without returning result). It is an expression, like if-else:

Let’s refactor our if-else example with the use of when/else:

Try/catch/finally

It is mostly the same behaviour as in Java:

try { // some code }
catch (e: SomeException) { // do something }
finally { // do something }

but it can be used as an expression:

More about exceptions:

Ranges

Kotlin lets you easily create ranges of values using the rangeTo() function from the kotlin.ranges package and its operator form ... Usually, rangeTo() is complemented by in or !in functions.

You can check more here:

LITSLINK

LITSLINK’s team is sharing their experience

Sign up for LITSLINK Newsletter

By LITSLINK

It's a test newsletter! Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Artem Diashkin

Written by

Java, Spring, Node.js, AdonisJs, React.js and Flutter developer

LITSLINK

LITSLINK

Hands-on up to date experience sharing of the most skilled & talented software developers right here, right now. Subscribe to learn & understand more about the Software World.

Artem Diashkin

Written by

Java, Spring, Node.js, AdonisJs, React.js and Flutter developer

LITSLINK

LITSLINK

Hands-on up to date experience sharing of the most skilled & talented software developers right here, right now. Subscribe to learn & understand more about the Software World.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store