Android and the browser. (http://kotlinlang.org/) Why Kotlin? • Concise: drastically reduce the amount of boilerplate code you need to write. • Safe: avoid entire classes of errors such as null pointer exceptions. • Interoperable: leverage existing frameworks and libraries of the JVM with 100% Java Interoperability. and more... http://kotlinlang.org/docs/reference/comparison-to-java.html
} public class MyJavaClass { private final int a = 1; public int getA() { return a; } } #1 Kotlin - Properties: val, var http://kotlinlang.org/docs/reference/properties.html
b: Int = 1 } public class MyJavaClass { private final int a = 1; private int b = 1; public int getA() { return a; } public int getB() { return b; } public void setB(int b) { this.b = b; } } #1 Kotlin - Properties: val, var http://kotlinlang.org/docs/reference/properties.html vs
b: Int = 1 val c = 1 var d = 1 } public class MyJavaClass { private final int a = 1; private int b = 1; private final int c = 1; private int d = 1; public int getA() { return a; } public int getB() { return b; } public void setB(int b) { this.b = b; } public int getC() { return c; } public int getD() { return d; } public void setD(int d) { this.d = d; } } #1 Kotlin - Properties: val, var http://kotlinlang.org/docs/reference/properties.html vs
"Miatello" val example = "My name is $name $surname" } class MyJavaClass { final String getName() { return "Omar"; } final String getSurname() { return "Miatello"; } final String getExample() { return String.format("My name is %s %s", getName(), getSurname()); } } #2 Kotlin - String templates http://kotlinlang.org/docs/reference/basic-syntax.html#using-string-templates vs
"Miatello" val example = "My name is $name $surname" } class MyJavaClass { final String getName() { return "Omar"; } final String getSurname() { return "Miatello"; } final String getExample() { return String.format("My name is %s %s", getName(), getSurname()); } } #2 Kotlin - String templates http://kotlinlang.org/docs/reference/basic-syntax.html#using-string-templates vs
3. Install “Kotlin” and “Kotlin Extensions For Android” 4. Restart Android Studio, and open (or create) a project 5. Create a new “Kotlin class” Install Kotlin in Android Studio
val content: String, val details: String) data class VillanItem(val name: String, val power: String) data class SpacecraftItem(val armaments: String, val defenses: String) data class TinyItem(val name: String) // ...
3. Install “Kotlin” and “Kotlin Extensions For Android” 4. Restart Android Studio, and open (or create) a project 5. Create a new “Kotlin class” 6. Choose from menu “Tools” > “Kotlin” > “Configure Kotlin in Project” Install Kotlin in Android Studio
HeroOnClickListener? // can be replaced with val mListener: Function1<HeroItem, Unit>? // OPTIMIZATION 2: Method definition override fun getItemCount(): Int { return mValues.size } // can be replaced with override fun getItemCount() = mValues.size
name, gender (String), power (Int) - Show item in list like: “$name (Power: $power)” - Choose (and keep in memory) hero “onClick” - Fight with second “onClick” (show a Snackbar) Hint: Add in MainActivity “selectedHero” property (in Kotlin, or use field in Java) Start with: https://github.com/jacklt/KotlinExample/tree/java-version or https://github.com/jacklt/KotlinExample/tree/kotlin-version Solution: https://github.com/jacklt/KotlinExample/tree/ex1