Digital Garden
Computer Science
Kotlin
From Java to Kotlin

From Java to Kotlin

Kotlin and the JVM

Kotlin was created by JetBrains in 2011 as an improved alternative to Java. Kotlin is more modern, concise, and safer than Java. Kotlin has gained a lot of popularity as it just like Java but better in many ways, Google even declared as the primary language for Android development.

Kotlin was designed to be fully interoperable with Java. This means that you can use Java classes, methods and libraries in Kotlin and vice versa. This is possible because Kotlin code can be compiler into Java bytecode. The Kotlin compiler generates .class files just like the Java compiler that can then be executed on the Java Virtual Machine (JVM).

Entry Point

Just like Java, Kotlin uses a main function as an entry point. However in Java it always needs to be able to handle the program arguments by using either of these possibilities:

public static void main(String args[]) {
    System.out.println("Hello World!");
} 

or

public static void main(String... args) {
    System.out.println("Hello World!");
} 

In Kotlin it is like in C you can either define the program arguements or not, if you don't define them they will just be ignored.

fun main() {
    println("Hello world!")
}

or

fun main(args: Array<String>) {
    println("Hello world!")
}

Where Array is a wrapper with a lot of additional functioanlity for a normal Java array.

Semicolons

Unlike in Java where semicolons are mandatory to end a statement, in Kotlin semicolons are optional. However, there are some situations where you may need to use semicolons in Kotlin to separate multiple statements on a single line.

Kotlin
val a = 0
val x = 5; val y = 10; println(x + y)
Java
final var a = 0;
final var x = 5; final var y = 10; System.out.println(x + y);

Standard Output

In Java the java.lang package is implicitly imported which contains amongst other things the classes for String, Integer and important in this section the System class which is used to output things to the standard output via System.out.println(). In Kotlin java.lang is implicitly imported but so is the kotlin package and more importantly for this section the kotlin.io package (plus a few other packages). The kotlin.io package contains, as with a lot of things in Kotlin the wrapper function println() which internally just calls System.out.println(). This then allows you to not always have to write such verbose code (you can still however of course use System.out.println() in kotlin, but don't).

Kotlin
println("hello world!")
Java
System.out.println("hello world!");

val and var

Type inference was introduced in Java 10 with the introduction of the var keyword. In Kotlin, you use var to define a mutable variable. By default, the type can be inferred, but it can also be explicitly defined after :. In Kotlin, you can also use the val keyword which the same as writing final var in Java, i.e. the variable is then immutable (read-only).

Warning

The final keyword exists in Kotlin, however can not be used in conjunction with variables, only to stop inheritance of a class method. I know very confusing.

Kotlin
    var a = 1
    var b // doesn't work, how much memory???
    var c : Int;
    c = 4
    var d : Int = 5
    val e = 10
    e = 15 // will fail
Java
var a = 1; // java also has type inference
var b; // also doesn't work
int c;
c = 4;
int d = 5;
final int e = 10;
e = 15; // will also fail