Котлин. In English, the name is usually pronounced “cot- lin,” not “coat-lin” or “caught-lin.” ▸ It is a statically typed programming language that runs on the JVM ▸ Created by JetBrains, first release - July 2011, latest release v1.1.4-3 - Aug 2017 ▸ It can be used for developing server-side, Android applications, and also provides the ability to target JavaScript
as Android Studio, IntelliJ IDEA ▸ Higher functions and lambda’s ▸ Class extensions ▸ Strongly typed with inferred data types and say goodbye to NullPointException ▸ Write more with much less code ▸ Generates very similar bytecode to Java ▸ 100% interoperable with Java and Android ▸ Now official on Android
= 1L + 3 // With type inferencing, Long + Int => Long val str: String = “string" // Without type inferencing // Non-Null type var a: String = "abc" a = null // compilation error // Nullable type var b: String? = "abc" b = null // ok val l = b.length // error: variable 'b' can be null // Safe Calls b?.length lateinit var presenter: DashboardPresenter
val tmp = this[index1] this[index1] = this[index2] this[index2] = tmp } val l = mutableListOf(1, 2, 3) l.swap(0, 2) // 'this' inside 'swap()' will hold the value of 'l'
@SerializedName("temp") val temperature: Float, @SerializedName("desc") val description: String) // that’s all the declaration val f1 = Forecast(Date(), 27.5f, "Storming") val f2 = f1.copy(temperature = 31f) // Destructuring Declarations val jane = User("Jane", 35) val (name, age) = jane println("$name, $age years of age") // prints "Jane, 35 years of age”
print("even") else -> print("x is weird") } when (x) { 0, 1 -> print("x == 0 or x == 1") else -> print("otherwise") } when (x) { parseInt(s) -> print("s encodes x") else -> print("s does not encode x") } when (x) { in 1..10 -> print("x is in the range") !in 10..20 -> print("outside the range") else -> print("none of the above") }
result = StringBuilder() for (letter in 'A'..'Z') { result.append(letter) } result.append("\nNow I know the alphabet!") return result.toString() } fun alphabet() = with(StringBuilder()) { for (letter in 'A'..'Z') { append(letter) } append("\nNow I know the alphabet!") toString() } fun alphabet() = StringBuilder().apply { for (letter in 'A'..'Z') { append(letter) } append("\nNow I know the alphabet!") }.toString()
status) = getResult() ▸ Shorter syntax for properties data class Person(val name: String, val age: Int) { val isAdult get() = age >= 20 // Property type inferred to be 'Boolean' }