Are you stuck in the Java world? I’ll share my story about convincing my team and the client of the benefits of Kotlin. Furthermore I’ll delve into how we migrated an existing Java Android app, with 300k active users, to Kotlin. Even if you have never seen Kotlin before, come and see how you will create better apps with this modern and elegant language. At the end of this talk you’ll be able to convince your team / client why it’s a great to use Kotlin. The power of Kotlin can be leveraged everywhere you use Java, since it compiles to JVM bytecode. So even if you’re not an Android developer, check out this session to get acquainted with Kotlin! No excuses: switch to Kotlin :)
13. class Person(val firstName: String,
val age: Int,
val locale: Locale = ENGLISH) {
fun sayHello() = println("Hi $firstName!")
}
Classes
val thijs = Person("Thijs", 34)
thijs.sayHello() // Hi Thijs!
class Cat(name: String) {
val name: String = name
}
14. val x: String? = getSomeOptionalString()
x.length // Does not compile.
Null safety / optionals
val y: String = null // Does not compile.
if (x != null) {
x.length // Compiles!
}
// Safe call
val optionalLength = x?.length
// Elvis operator.
val length = x?.length ?: -1
15. Data classes
class User(val name: String, val age: Int)
val thijs = User("Thijs", 35)
println(thijs) // User(name=Thijs, age=35)
val bday = thijs.copy(age = 36) // Named params!
println(bday) // User(name=Thijs, age=36)
data
16. Data classes
public class User {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
if (age != user.age) return false;
return name != null ? name.equals(user.name) : user.name == null;
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + age;
return result;
}
@Override
public String toString() {
return "User{" +
"name='" + name + ''' +
", age=" + age +
'}';
}
}
17. Functional / lambas
val sum = listOf(1, 2, 3, 4, 5)
.filter { number -> number < 5 }
.map { it * 2 }
.sum()
println(sum) // 20
listOf("Zack", "Bob", "Thijs", "Julie", "Rob")
.filter { it.length > 3 }
.sorted()
.forEach { println(it) } // Julie Thijs Zack
19. When statement
// Java
switch (firstName) {
case "Dan":
case "Jay":
person.setTeam(programmers);
break;
case "Jamie":
person.setTeam(designers);
break;
default:
person.setTeam(others);
}
// Kotlin
when (firstName) {
"Dan", "Jay" -> person.team = programmers
"Jamie" -> person.team = designers
else -> person.team = others
}
20. When statement
when (x) {
1 -> print("x = 1")
is Int -> print(x + 1)
is String -> print(x.length + 1)
is IntArray -> print(x.sum())
in 10..20 -> print("Between 10 and 20")
}
val outcome = when(x) {
1 -> "x = 1"
"Hello" -> "is it me you're looking for?"
in 10..20 -> "Between 10 and 20"
}
21. Extensions
// Extension function
fun String.last(): Char {
return this[lastIndex]
}
println("Hello!".last())
// Extension property
val String.first: Char get() = this[0]
println("Hello! ".first) // H