new language for the JVM • Designed by: JetBrains • Version v1.0: was released on February 15, 2016 • Stable release: Kotlin 1.0.6 / December 27, 2016 • License: Apache 2 • Filename extension: .kt .kts • Preview release: Kotlin 1.1.0-beta-17 / January 19, 2017;
{ if (args.size < 2) { print("Two integers expected") return } val x = parseInt(args[0]) val y = parseInt(args[1]) // Using `x * y` yields error because they may hold nulls. if (x != null && y != null) { // x and y are automatically cast to non-nullable after null check print(x * y) } }
{ if (obj is String) { // `obj` is automatically cast to `String` in this branch return obj.length } // `obj` is still of type `Any` outside of the type-checked branch return null }
Int) provides a User class with the following functionality: • getters (and setters in case of vars) for all properties • equals() • hashCode() • toString() • copy() • component1(), component2(), …, for all properties
closed range: includes 100 for (i in 1 until 100) { ... } // half-open range: does not include 100 for (x in 2..10 step 2) { ... } for (x in 10 downTo 1) { ... } if (x in 1..10) { ... }
fun penDown() fun penUp() fun turn(degrees: Double) fun forward(pixels: Double) } val myTurtle = Turtle() with(myTurtle) { //draw a 100 pix square penDown() for(i in 1..4) { forward(100.0) turn(90.0) } penUp() }
type information // public final class Gson { // ... // public <T> T fromJson(JsonElement json, Class<T> classOfT) throws JsonSyntaxException { // ... inline fun <reified T: Any> Gson.fromJson(json): T = this.fromJson(json, T::class.java)