Contents

Kotlin Basics: syntax

Contents

Kotlin is a great language but it’s more like a subset of Java.

In this article, I will deal with Kotlin basics and see what’s different with Java in terms of syntax, form and types.

Before jumping to the subject, let’s see how Kotlin syntax differs from Java syntax.

Packages and imports

Packages and imports are the same in Kotlin and Java. So let’s skip those.

Functions

In Java, declaring a function must follow some directives:

1
2
3
<visibility> <static> <returnType> <functionName>(<argumentType> <argumentName>) {
    // Content of the function
}

Moreover, you need to create a class in order to create a function. So it’s something like this:

1
2
3
4
5
public class Foobar {
    public static void main(String[] args) {
        System.out.println("Hello world");
    }
}

Whereas in Kotlin, you do not need to create a class as it’s not relevant to any class which is more logical. When declaring a function, it must follow the following:

1
2
3
fun <functionName>(<argumentName>: <argumentType>): <returnType> {
    // Content of the function
}

So for the same example as Java, it will be something like this in Kotlin:

1
2
3
fun main(args: Arrays<String>) {
    println("Hello world")
}

You might notice several aspects that differ from Java:

  • Functions in Kotlin are declared using the fun keyword
  • I did not define the return type of the function: it’s implicit, meaning if not defined, then it’s a void or Unit in Kotlin
  • I noticed that recent languages declare the argument name first before the argument type, so it’s a bit like Golang
  • You do not need to call System.out in order to print a message, just call print or println
  • There are NO ;!!! No need to waste time looking for missing ; !

However, it’s not the only way to declare a function in Kotlin:

1
2
3
4
5
fun sum(a: Int, b: Int) = a + b
// is equivalent to
fun sum(a: Int, b: Int): Int {
    return a + b
}
  • The return type is not defined, it’s also implicit and it will automatically detect the type from the return type (ie: type inference)
  • It’s declared like assigning a variable, but it’s used like a real function:
1
2
3
4
fun main(args: Array<String> {
    val result = sum(88, 12)
    println("Sum of 88 and 12 is ${result}")
}

Variables

In Java, when you are declaring a variable, you MUST declare its type:

1
String message = "Hello world!";

Whereas in Kotlin, the type inference is really strong:

1
2
3
4
5
// Read-only variable declaration
val message = "Hello world" // Kotlin "knows" it's a String
// Mutable variable
var a = 1
a += 2
Type inference

Comments

Comments are about the same in Java and Kotlin.

1
2
3
4
// This is an end-of-line comment

/* This is a block comment
   on multiple lines. */

String interpolation

In Java, when building a String, it’s a bit messy:

1
2
3
4
int a = 1;
int b = 2;
int result = a + b;
String message = "Hello world, this is the result of the computation of " + a + " and " + b + " which gives " + result;

In kotlin, you can use the $ along with { } to fetch its data:

1
2
3
val a = 1
val b = 2
val message = "Hello world, this is the result of the computation of $a and $b which gives ${a + b}"

Classes

Declaring a class in Java is done like this:

1
2
public class Foobar {
}

In Kotlin, it’s about the same:

1
2
class Foobar {
}

However, when declaring a new instance of the class:

1
Foobar foobar = new Foobar();

Whereas in Kotlin, you don’t need the new keyword:

1
var foobar = Foobar()

For loop

1
2
3
4
5
6
for (int i = 0; i < 10; i++) {
    System.out.println(i);
}
for (int j = 0; j < 10; j++) {
    System.out.println(j);
}
1
2
3
4
5
6
for (i in 0..9) {
    println(i)
}
for (j in 0..9 step 2) {
    println(j)
}
  • It’s more readable in Kotlin than in Java
  • We do not need to specify the var or val keywords, again, it’s implicit

Conclusion

So far, Kotlin brings a lot of “common sense”:

  • clever type inference
  • removal of the new keyword
  • better function declarations
  • readable String templates
  • readable for loops

Good and maintainable codes are human readable codes