+1

Awesome Kotlin [begining]

Kotlin is a statically typed programming language for the JVM, Android and the browser. Designed by JetBrains, the maker of the world’s best IDE’s. 100% interoperable with Java. Can be compiled to ByteCode and also to JavaScript

In this article i want to show several examples which impressed me, when i saw Kotlin fist time. Let's start

Shortest syntax

Here we have simple, very popular Java class structure:

public class User {
    public String firstName;
    public String lastName;
    public Integer age;

    public User(String firstName, String lastName, Integer age) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public Integer getAge() {
        return age;
    }
}

What are doing this class? Exactly - nothing, but it's difficult to fit on the screen.

Now let’s write this class in a Kotlin:

class User(val firstName: String, val lastName: String, val age: Int) { }

Just one string! Awesome, isn't it?

Let's see what's going on here:

  • First you can see the brackets directly after class name - its a simple constructor with 3 parameters which will be set to properties. more...
  • In a second, it's a val keyword - that's means what it's immutable property, have only getter. If you want getter and setter - use var keyword. more...
  • Also u can see what type of variable sets after name through the : symbol. more...
  • Kotlin also automatically makes a getters for it.

Null safety

Kotlin, like C#, also keep null information about types in his boxes. But Kotlin have some syntax sugar for awesome null checking.

Let see an example of Java code:

class User {
    // ...
    public Website website;
}

class Website {
    public String url;
}

class Test {
    public static void main(String[] args) {
        User user = new User();
        System.out.println(user.website.url); // Runtime NPE
    }
}

Here we can see what compiler said "code is OK", but on runtime we getting a NullPointerException.

All of as solve it like this:

class Test {
    public static void main(String[] args) {
        User user = new User("John", "Doe", 30);
        if (user.website != null) {
            System.out.println(user.website.url);
        }
    }
}

Every time we should not forget about null-checking, and compiler is not help to as. But Kotlin provide an ability to declare a nullable type via adding ? symbol after type.

Let see our example in a Kotlin:

class User(/* ... */) {
    var website: Website? = null
}

class Website {
    var url: String? = null
}

class Test {
    fun main(args: Array<String>) {
        val user = User("John", "Doe", 30)
        println(user.website?.url) // print null
    }
}

Here u can see Declaring of nullable type and construction like a ?. in here user.website?.url. That's mean what if website is null all expression returns null, if not url value will be returned.

Also Kotiln have this construction println(user.website!!.url). That's mean - OK lets be NPE more...

Can say more, ?. and !!. it's not a runtime magic, it's an operator and can be overloaded, but this i try to represent in a next articles.

If you are interested of it, you can try it here online. Full documentation you can find here. All news can be founded in a Kotlin blog

Be awesome, use Kotlin!kotlin_250x250.png


All Rights Reserved

Viblo
Let's register a Viblo Account to get more interesting posts.