Kotlin 1.0!

WHAT?

Pragmatic Language for JVM and Android

Why?

  • interoperability
  • safety
  • clarity
  • tooling

interoperability

// KotlinFile.kt

data class Niceeee(val such: String, val so: String)

fun main(args: Array<String>) {
    val nice1 = Niceeee("nice", "cool")
    val nice2 = Niceeee(such = "nice", so = "cool")
    println(nice1)
    println(nice2)
    println(nice1 == nice2)
}

// Niceeee(such=nice, so=cool)
// Niceeee(such=nice, so=cool)
// true

Kotlin

// JavaClass.java

public class JavaClass {
    public static void main(String[] args) {
        Niceeee niceeee1 = new Niceeee("nice", "cool");
        Niceeee niceeee2 = new Niceeee("nice", "cool");
        System.out.println(niceeee1);
        System.out.println(niceeee2);
        System.out.println(niceeee1.equals(niceeee2));
    }
}

// Niceeee(such=nice, so=cool)
// Niceeee(such=nice, so=cool)
// true

Java

Data Class

// KotlinFile.kt

fun main(args: Array<String>) {
    println("Hello, World".allBetterWithBang())
}

fun String.allBetterWithBang(): String {
    return "${this}!"
}

// Hello, World!

Kotlin

// JavaClass.java

public class JavaClass {
    public static void main(String[] args) {
        System.out.println(KotlinFileKt.allBetterWithBang("Hello, World"));
    }
}


// Hello, World!

Java

Extension Functions

// KotlinFile.kt

object Lonely {
    var state = "I hold state!"
}

Kotlin

// JavaClass.java

public class JavaClass {
    public static void main(String[] args) {
        Lonely.INSTANCE.getState();
        Lonely.INSTANCE.setState("No! Java with you!");
    }
}

Java

Singleton

safety

// KotlinFile.kt

fun main(args: Array<String>) {
    val str: String = null
}

// Kotlin: Null can not be a value of a non-null type kotlin.String

Kotlin

// JavaClass.java

public class JavaClass {
    public static void main(String[] args) {
        String str = null;
        str.length();
    }
}

// Java: Ok!

Java

Nope! Pointer Exceptions

// KotlinFile.kt

fun main(args: Array<String>) {
    val str: String? = null // Kotlin: Ok!
}

Kotlin

Nope! Pointer Exceptions

// KotlinFile.kt

fun main(args: Array<String>) {
    val str: String? = null

    str.length
}

// Kotlin: Only safe (?.) or non-null asserted (!!.) calls are allowed on 
//   a nullable receiver of type kotlin.String?

Kotlin

Nope! Pointer Exceptions

// Elvis
fun main(args: Array<String>) {
    val str: String? = null

    val nonNull = str?.length ?: 0

    println(nonNull)
}

// 0
// Safe-call
fun main(args: Array<String>) {
    val str: String? = null

    println(str?.length)
}

// null
// Extension function
fun main(args: Array<String>) {
    val str: String? = null

    str?.let {
        println(str.length)
    }
}

// nothing

Java

Cast?

public class JavaClass {
    public static void main(String[] args) {
        Object master = "Yoda";
        
        if (master instanceof String) {
            System.out.println(((String) master).length());
        }
    }
}

// 4

Kotlin

fun main(args: Array<String>) {
    val master: Any = "Yoda"

    if (master is String) {
        println(master.length)
    }
}

// 4

clarity

Kotlin

data class User(val name: String, val age: Int)
val (name, age) = person
for ((name, age) in persons) {
     // ...
}
fun function(...): Person {
    // computations
    
    return person
}

// Now, to use this function:
val (name, age) = function(...)

And more, and more...

tooling

  • Idea (Ultimate and Community)
  • Android Studio
  • Eclipse
  • Gradle, Maven, Ant and Kobalt

Try it!

try.kotl.in

Awesome Kotlin

http://kotlin.link

Quiz

In which Class PSVM?

// File.kt

package foo.bar.test

fun main(args: Array<String>) {
    println("Hello, World!")
}
foo.bar.test.FileKt.main(...);