• A megszokott környezetben használható
• Android Studio plugin
• 4 sor a Gradle build fájlokba
Kis méretű runtime és stdlib (<1 MB)
• Fájlonként adoptálható
Új kód Kotlinban
Meglévő Java kód refaktorálása
Használat
Slide 4
Slide 4 text
Kotlin gyorstalpaló
Slide 5
Slide 5 text
Változók
// Java
int x = 1;
final int x = 1;
// Kotlin
var x: Int = 1
val x = 1
Slide 6
Slide 6 text
Függvények
fun add(a: Int, b: Int): Int = a + b
fun add(a: Int, b: Int): Int {
return a + b
}
fun add(a: Int, b: Int) = a + b
Slide 7
Slide 7 text
Osztályok
class Person(val name: String, var age: Int) {
}
Slide 8
Slide 8 text
Az osztály Java megfelelője
public class Person {
private final String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Slide 9
Slide 9 text
Data class
class Person(val name: String, var age: Int)
data
Slide 10
Slide 10 text
A data class Java megfelelője
public class Person {
private final String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass())
return false;
Person person = (Person) o;
if (age != person.age) return false;
return name != null ? name.equals(person.name)
: person.name == null;
}
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + age;
return result;
}
}
Slide 11
Slide 11 text
Data class - összefoglaló
data class Person(
val name: String,
var age: Int)
public class Person {
private final String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
if (age != person.age) return false;
return name != null ? name.equals(person.name) : person.name == null;
}
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + age;
return result;
}
}
Slide 12
Slide 12 text
Példák
Slide 13
Slide 13 text
Property hozzáférések
data class Person(val name: String, var age: Int)
// Kotlin
val person = Person("Sam", 41)
println(person.name)
person.age = 42
Slide 14
Slide 14 text
Property hozzáférések
data class Person(val name: String, var age: Int)
// Java
Person person = new Person("Sam", 41);
System.out.println(person.getName());
person.setAge(42);
• JVM
Android
Desktop
Szerver
• Gradle Script Kotlin
• JavaScript (1.1 óta stabil)
Kliens oldal
Szerver oldal
• (Native)
Több, mint egy “jobb Java”
android {
buildToolsVersion("25.0.0")
compileSdkVersion(23)
defaultConfig {
minSdkVersion(15)
targetSdkVersion(23)
applicationId = "com.ex.app"
versionCode = 1
versionName = "1.0"
}
}
Slide 31
Slide 31 text
• kotlinlang.org
Merre tovább?
Slide 32
Slide 32 text
Merre tovább?
• Kotlin – Ready for Production (Hadi Hariri)
https://youtu.be/R0J_Jl7bKY8
• Android Development with Kotlin (Jake Wharton)
https://youtu.be/A2LukgT2mKc
• Kotlin in Production (Christina Lee)
https://youtu.be/mDpnc45WwlI
• 10 Kotlin Tricks in 10(ish) Minutes (Jake Wharton)
https://youtu.be/YKzUbeUtTak
Slide 33
Slide 33 text
Merre tovább?
• Using Project Kotlin for Android (Jake Wharton)
https://docs.google.com/document/d/1ReS3ep-
hjxWA8kZi0YqDbEhCqTt29hG8P44aA9W0DM8/preview
• Why you should totally switch to Kotlin (Magnus Vinther)
https://medium.com/@magnus.chatt/why-you-should-totally-
switch-to-kotlin-c7bbde9e10d5
• What do 17 Google Developers Experts for Android think
about Kotlin? (Antonio Leiva)
https://antonioleiva.com/google-kotlin/
Slide 34
Slide 34 text
Merre tovább?
• Kotlin on Android. Now official (JetBrains, Kotlin Blog)
https://blog.jetbrains.com/kotlin/2017/05/kotlin-on-android-
now-official/
• Kotlin and Android | Android Developers
https://developer.android.com/kotlin/index.html
• Kotlin on Android FAQ (Google)
https://developer.android.com/kotlin/faq.html
Slide 35
Slide 35 text
Merre tovább?
• Introduction to Kotlin (Hadi Hariri, Andrey Breslav)
Péntek (május 19.), 19:30-20:30
• Life is great and everything will be ok, Kotlin is here
(Christina Lee, Jake Wharton)
Péntek (május 19.), 23:30-00:30
Slide 36
Slide 36 text
Amire nem jutott idő…
• Null safety
• Collection manipulations
• Inline and infix functions
• Named and default parameters
• Sealed class, when expression
• Declaration site variance
• Destructuring declarations
• Operator overloading
• Delegation
• String templates
• Sequences
• Singletons
• Coroutines
• …
Slide 37
Slide 37 text
Köszönöm a figyelmet!
zsmb13
[email protected] zsmb13
Fotó:
Alexey Sergeev