!= null) b.length else -1 Null Safe (3) ~ Elvis Operator val l = b?.length ?: -1 Elvis Operator https://kotlinlang.org/docs/reference/null-safety.html#elvis-operator
// String val c = 'V' // Char val i = 10 // Int val l = 10L // Long val f = 10F // Float val d = 10.0 // Double val b = true // Boolean val sb = StringBuilder() // StringBuilder
try) int a = 10; String value; if (a > 10) { value = "Over 10"; } else { value = "Under 10"; } val a = 10 val value: String if (a > 10) { value = "Over 10" } else { value = "Under 10" } Java Kotlin
try) int a = 10; String value; if (a > 10) { value = "Over 10"; } else { value = "Under 10"; } val a = 10 val value = if (a > 10) { "Over 10" } else { "Under 10" } Java Kotlin
try) String value; switch (a) { case 1: value = "one"; break; case 2: value = "two"; break; case 3: value = "three"; break; default: value = "another"; break; } val value: String when { a === 1 -> value = "one" a === 2 -> value = "two" a === 3 -> value = "three" else -> value = "another" } Java Kotlin
encodes x") in 1..10 -> print("x is in the range") in validNumbers -> print("x is valid") !in 10..20 -> print("x is outside the range") else -> print("none of the above") } Control Flow (if / when / try) Kotlin
encodes x") in 1..10 -> print("x is in the range") in validNumbers -> print("x is valid") !in 10..20 -> print("x is outside the range") else -> print("none of the above") } Control Flow (if / when / try) Kotlin Expression
encodes x") in 1..10 -> print("x is in the range") in validNumbers -> print("x is valid") !in 10..20 -> print("x is outside the range") else -> print("none of the above") } Control Flow (if / when / try) Kotlin Range
encodes x") in 1..10 -> print("x is in the range") in validNumbers -> print("x is valid") !in 10..20 -> print("x is outside the range") else -> print("none of the above") } Control Flow (if / when / try) Kotlin Not in Range
encodes x") in 1..10 -> print("x is in the range") in validNumbers -> print("x is valid") !in 10..20 -> print("x is outside the range") else -> print("none of the above") } Control Flow (if / when / try) Kotlin Optional, statement / expression
static final A instance = new A(); private A() {} public static A getInstance() { return instance; } public void b() { System.out.println("Singleton"); } } Java // Use A.getInstance().b()
Unit = activity.toast(message) fun Context.toast(message: CharSequence) = Toast.makeText(this, message, Toast.LENGTH_SHORT).show() https://github.com/Kotlin/anko/blob/d5a526512b48c5cd2e3b8f6ff14b153c2337aa22/anko/library/ static/commons/src/dialogs/Toasts.kt // Use in Activity/Fragment toast("Hello World")
R> List<T>.map(transform: (T) -> R): List<R> { val result = arrayListOf<R>() for (item in this) result.add(transform(item)) return result } Function Type
public class MyParcelable implements Parcelable { private int mData; public int describeContents() { return 0; } public void writeToParcel(Parcel out, int flags) { out.writeInt(mData); } public static final Parcelable.Creator<MyParcelable> CREATOR = new Parcelable.Creator<MyParcelable>() { public MyParcelable createFromParcel(Parcel in) { return new MyParcelable(in); } public MyParcelable[] newArray(int size) { return new MyParcelable[size]; } }; private MyParcelable(Parcel in) { mData = in.readInt(); } } https://github.com/Kotlin/KEEP/blob/master/proposals/extensions/android-parcelable.md
Get Started with Kotlin — It is here to stay (https://medium.com/peachstudio/get-started- with-kotlin-it-is-here-to-stay-1eaac85ae6a0) ▸ Kotlin: A New Hope in a Java 6 Wasteland (https://academy.realm.io/posts/droidcon- michael-pardo-kotlin/) ▸ Kotlin ࡄҗ Ӓܿ (http://tech.lezhin.com/2017/08/03/the-case-against-kotlin) ▸ Beyond Kotlin - Advanced features for API Makers (https://speakerdeck.com/agiuliani/ beyond-kotlin-advanced-features-for-api-makers) ▸ Develop your next app with Kotlin - AndroidRennes 2017 (https://speakerdeck.com/agiuliani/ develop-your-next-app-with-kotlin-androidrennes-2017)