*.java
*.class
*.dex
compiled to Java bytecode
*.kt
Slide 10
Slide 10 text
Kotlin code
Java code
You can have Java &
Kotlin code in one project
Slide 11
Slide 11 text
You can gradually add
Kotlin to your existing app
Slide 12
Slide 12 text
Android-friendly
Slide 13
Slide 13 text
Android Studio
is based on
IntelliJ IDEA
Slide 14
Slide 14 text
just another library
for your app
rxjava-2.1.2
kotlin-stdlib-1.1.4 6315
10212
Slide 15
Slide 15 text
No Kotlin SDK
…just JDK + extensions
small runtime jar easy Java interop
Slide 16
Slide 16 text
Gradle & Kotlin
Writing Gradle build scripts
and plugins in Kotlin
Slide 17
Slide 17 text
try.kotlinlang.org
Slide 18
Slide 18 text
Kotlin Koans
Slide 19
Slide 19 text
No content
Slide 20
Slide 20 text
J2K converter
Slide 21
Slide 21 text
public class Person {
private final String name;
private final int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
Slide 22
Slide 22 text
- equals
- hashCode
- toString
data
class Person(val name: String, val age: Int)
Slide 23
Slide 23 text
public class Person {
private final String name;
private final int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
class Person(
val name: String,
val age: Int
)
person.name
person.getName()
Slide 24
Slide 24 text
class Person(
val name: String,
val age: Int
)
person.getName()
Slide 25
Slide 25 text
public class Person {
private final String name;
private final int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
person.name
fun updateWeather(degrees: Int) {
val description: String
val colour: Colour
if (degrees < 5) {
description = "cold"
colour = BLUE
} else if (degrees < 23) {
description = "mild"
colour = ORANGE
} else {
description = "hot"
colour = RED
}
// ...
}
Slide 28
Slide 28 text
fun updateWeather(degrees: Int) {
val (description: String, colour: Colour) =
if (degrees < 5) {
Pair("cold", BLUE)
} else if (degrees < 23) {
Pair("mild", ORANGE)
} else {
Pair("hot", RED)
}
// ...
}
Slide 29
Slide 29 text
fun updateWeather(degrees: Int) {
val (description, colour) =
if (degrees < 5) {
Pair("cold", BLUE)
} else if (degrees < 23) {
Pair("mild", ORANGE)
} else {
Pair("hot", RED)
}
// ...
}
Slide 30
Slide 30 text
fun updateWeather(degrees: Int) {
val (description, colour) = when {
degrees < 5 -> Pair("cold", BLUE)
degrees < 23 -> Pair("mild", ORANGE)
else -> Pair("hot", RED)
}
// ...
}
Slide 31
Slide 31 text
fun updateWeather(degrees: Int) {
val (description, colour) = when {
degrees < 5 -> "cold" to BLUE
degrees < 23 -> "mild" to ORANGE
else -> "hot" to RED
}
}
Slide 32
Slide 32 text
val (description, colour) = when {
degrees < 5 -> "cold" to BLUE
degrees < 23 -> "mild" to ORANGE
else -> "hot" to RED
}
String description;
Colour colour;
if (degrees < 5) {
description = "cold";
colour = BLUE;
} else if (degrees < 23) {
description = "mild";
colour = ORANGE;
} else {
description = "hot";
colour = RED;
}
Slide 33
Slide 33 text
Extension Functions
Slide 34
Slide 34 text
fun String.lastChar() = get(length - 1)
this can be omitted
Extension Functions
fun String.lastChar() = this.get(this.length - 1)
Slide 35
Slide 35 text
import com.example.util.lastChar
import com.example.util.*
Extension Functions
val c: Char = "abc".lastChar()
fun String.lastChar() = get(length - 1)
Slide 36
Slide 36 text
fun String.lastChar() = get(length - 1)
Calling Extension Functions
from Java code
StringExtensions.kt
char c = StringExtensionsKt.lastChar("abc");
JavaClass.java
import static StringExtensionsKt.lastChar;
char c = lastChar("abc");
Slide 37
Slide 37 text
No. Because it’s a regular
static method under the hood.
fun String.lastChar() = get(length - 1)
Extension Functions
Is it possible to call a private
member of String here?
infix fun A.to(that: B) = Pair(this, that)
"ANSWER".to(42)
"hot" to RED
mapOf(0 to "zero", 1 to "one")
The to extension function
Slide 40
Slide 40 text
Extensions for Android
Toast.makeText(this, "Thank you!”, Toast.LENGTH_SHORT).show()
Activity
extension function
on Activity
toast("Thank you!")
this.toast("Thank you!")
Slide 41
Slide 41 text
this.startActivity("ANSWER" to 42)
val intent = Intent(this, NewActivity::class.java)
intent.putExtra("ANSWER", 42)
startActivity(intent)
Extensions for Android