Slide 1

Slide 1 text

Kotlin ve Java’nin yeni versiyonları Hadi Tok @hadi_tok Technical Lead @CitizenMe

Slide 2

Slide 2 text

About Kotlin Kotlin is a cross-platform, statically typed, general-purpose programming language with type inference. Kotlin is designed to interoperate fully with Java. Kotlin mainly targets the JVM, but also compiles to JavaScript or native code. Source: wikipedia.org / License: WP:CC BY-SA

Slide 3

Slide 3 text

About Kotlin Kotlin is a cross-platform, statically typed, general-purpose programming language with type inference. Kotlin is designed to interoperate fully with Java. Kotlin mainly targets the JVM, but also compiles to JavaScript or native code. Source: wikipedia.org / License: WP:CC BY-SA

Slide 4

Slide 4 text

About Kotlin Kotlin is a cross-platform, statically typed, general-purpose programming language with type inference. Kotlin is designed to interoperate fully with Java. Kotlin mainly targets the JVM, but also compiles to JavaScript or native code. Source: wikipedia.org / License: WP:CC BY-SA

Slide 5

Slide 5 text

About Kotlin Kotlin is a cross-platform, statically typed, general-purpose programming language with type inference. Kotlin is designed to interoperate fully with Java. Kotlin mainly targets the JVM, but also compiles to JavaScript or native code. Source: wikipedia.org / License: WP:CC BY-SA

Slide 6

Slide 6 text

About Kotlin Kotlin is a cross-platform, statically typed, general-purpose programming language with type inference. Kotlin is designed to interoperate fully with Java. Kotlin mainly targets the JVM, but also compiles to JavaScript or native code. Source: wikipedia.org / License: WP:CC BY-SA

Slide 7

Slide 7 text

About Kotlin Kotlin is a cross-platform, statically typed, general-purpose programming language with type inference. Kotlin is designed to interoperate fully with Java. Kotlin mainly targets the JVM, but also compiles to JavaScript or native code. Source: wikipedia.org / License: WP:CC BY-SA

Slide 8

Slide 8 text

Why Kotlin? Concise Drastically reduce the amount of boilerplate code Source: kotlinlang.org /License: Apache 2.0

Slide 9

Slide 9 text

Why Kotlin? Safe Avoid entire classes of errors such as null pointer exceptions Source: kotlinlang.org /License: Apache 2.0 Source: kotlinlang.org /License: Apache 2.0

Slide 10

Slide 10 text

Why Kotlin? Interoperable Leverage existing libraries for the JVM, Android, and the browser Source: kotlinlang.org /License: Apache 2.0

Slide 11

Slide 11 text

Kotlin for Java Programmers

Slide 12

Slide 12 text

Functions

Slide 13

Slide 13 text

fun main() { println("Hello world!") } Source: kotlinlang.org /License: Apache 2.0

Slide 14

Slide 14 text

fun sum(a: Int, b: Int): Int { return a + b } Source: kotlinlang.org /License: Apache 2.0

Slide 15

Slide 15 text

fun sum(a: Int, b: Int): Int { return a + b } fun sum(a: Int, b: Int) = a + b Source: kotlinlang.org /License: Apache 2.0

Slide 16

Slide 16 text

Default argumnets

Slide 17

Slide 17 text

fun namePrinter(firstName: String, lastName: String) { print("$firstName $lastName") } namePrinter("Hadi", "Tok")

Slide 18

Slide 18 text

fun namePrinter(firstName: String, lastName: String="Tok") { print("$firstName $lastName") } namePrinter("Hadi")

Slide 19

Slide 19 text

Named argumnets

Slide 20

Slide 20 text

fun namePrinter(firstName: String="Hadi", lastName: String="Tok") { print("$firstName $lastName") } namePrinter(lastName = "Hariri")

Slide 21

Slide 21 text

infix notation

Slide 22

Slide 22 text

class MyStringCollection { infix fun add(s: String) { /*...*/ } fun build() { this add "abc" // Correct add("abc") // Correct //add "abc" Incorrect: the receiver must be specified } } Source: kotlinlang.org /License: Apache 2.0

Slide 23

Slide 23 text

Extension Functions

Slide 24

Slide 24 text

fun MutableList.swap(index1: Int, index2: Int) { val tmp = this[index1] // 'this' corresponds to the list this[index1] = this[index2] this[index2] = tmp } Source: kotlinlang.org /License: Apache 2.0

Slide 25

Slide 25 text

Higher Order Functions

Slide 26

Slide 26 text

class Request { void longRunningOperation(Callback callback) { String result = "result ok"; //assume long running callback.getResult(result); } interface Callback { void getResult(String result); } }

Slide 27

Slide 27 text

Request request =new Request(); request.longRunningOperation(new Callback() { @Override public void getResult(String result) { System.out.println(result); } });

Slide 28

Slide 28 text

class Request{ fun longRunningOperation(callback:(String)->Unit){ val result = "result ok" //assume long running callback(result) } }

Slide 29

Slide 29 text

val request= Request() request.longRunningOperation({result-> print(result) })

Slide 30

Slide 30 text

val request= Request() request.longRunningOperation { result-> print(result) }

Slide 31

Slide 31 text

val request = Request() request.longRunningOperation(this::printResult) fun printResult(result:String){ print(result) }

Slide 32

Slide 32 text

Lambda expressions

Slide 33

Slide 33 text

val sum: (Int, Int) -> Int = { x: Int, y: Int -> x + y } Source: kotlinlang.org /License: Apache 2.0

Slide 34

Slide 34 text

val sum: (Int, Int) -> Int = { x: Int, y: Int -> x + y } sum(3,4) Source: kotlinlang.org /License: Apache 2.0

Slide 35

Slide 35 text

Variables

Slide 36

Slide 36 text

val a: Int = 1 // immediate assignment val b = 2 // `Int` type is inferred Source: kotlinlang.org /License: Apache 2.0

Slide 37

Slide 37 text

var x = 5 // `Int` type is inferred x += 1 Source: kotlinlang.org /License: Apache 2.0

Slide 38

Slide 38 text

Null Safety

Slide 39

Slide 39 text

var a: String = "abc" a = null // compilation error Source: kotlinlang.org /License: Apache 2.0

Slide 40

Slide 40 text

var b: String? = "abc" b = null // ok Source: kotlinlang.org /License: Apache 2.0

Slide 41

Slide 41 text

val l: Int = a.length Source: kotlinlang.org /License: Apache 2.0

Slide 42

Slide 42 text

val l: Int = a.length val m = b.length // error: variable 'b' can be null Source: kotlinlang.org /License: Apache 2.0

Slide 43

Slide 43 text

val c: String? = null val l = if (c != null) c.length else -1 Source: kotlinlang.org /License: Apache 2.0

Slide 44

Slide 44 text

val a = "Kotlin" val b: String? = null println(b?.length) println(a?.length) // Unnecessary safe call Source: kotlinlang.org /License: Apache 2.0

Slide 45

Slide 45 text

val listWithNulls: List = listOf("Kotlin", null) for (item in listWithNulls) { item?.let { println(it) } //prints Kotlin and ignores null } Source: kotlinlang.org /License: Apache 2.0

Slide 46

Slide 46 text

val l = if (c != null) c.length else -1 Source: kotlinlang.org /License: Apache 2.0

Slide 47

Slide 47 text

val l = if (c != null) c.length else -1 val l = c?.length ?: -1 Source: kotlinlang.org /License: Apache 2.0

Slide 48

Slide 48 text

var b: String? = "abc" val l = b!!.length Source: kotlinlang.org /License: Apache 2.0

Slide 49

Slide 49 text

String Templates

Slide 50

Slide 50 text

var a = 1 // simple name in template: val s1 = "a is $a" a = 2 // arbitrary expression in template: val s2 = "${s1.replace("is", "was")}, now is $a" Source: kotlinlang.org /License: Apache 2.0

Slide 51

Slide 51 text

String Literals

Slide 52

Slide 52 text

val s = "Hello, world!\n" Source: kotlinlang.org /License: Apache 2.0

Slide 53

Slide 53 text

val s = "Hello, world!\n" val text = """ for (c in "foo") print(c) """ Source: kotlinlang.org /License: Apache 2.0

Slide 54

Slide 54 text

val s = "Hello\nworld!" val text = """ for (c in "foo") print(c) """ val text = """ |Tell me and I forget. |Teach me and I remember. |Involve me and I learn. |(Benjamin Franklin) """.trimMargin() Source: kotlinlang.org /License: Apache 2.0

Slide 55

Slide 55 text

Type checks and automatic casts

Slide 56

Slide 56 text

if (obj is String) { // `obj` is automatically cast to `String` in // this branch return obj.length } Source: kotlinlang.org /License: Apache 2.0

Slide 57

Slide 57 text

if (obj !is String) return null // `obj` is automatically cast to `String` in // this branch return obj.length Source: kotlinlang.org /License: Apache 2.0

Slide 58

Slide 58 text

// `obj` is automatically cast to `String` on the // right-hand side of `&&` if (obj is String && obj.length > 0) { return obj.length } Source: kotlinlang.org /License: Apache 2.0

Slide 59

Slide 59 text

Loops

Slide 60

Slide 60 text

val items = listOf("apple", "banana") for (item in items) { println(item) } Source: kotlinlang.org /License: Apache 2.0

Slide 61

Slide 61 text

for (x in 1..5) { print(x) } Source: kotlinlang.org /License: Apache 2.0

Slide 62

Slide 62 text

for (x in 1..5) { print(x) } for (x in 1..10 step 2) { print(x) } Source: kotlinlang.org /License: Apache 2.0

Slide 63

Slide 63 text

for (x in 1..5) { print(x) } for (x in 1..10 step 2) { print(x) } for (x in 9 downTo 0 step 3) { print(x) } Source: kotlinlang.org /License: Apache 2.0

Slide 64

Slide 64 text

val items = listOf("apple", "banana") var index = 0 while (index < items.size) { println("item at $index is ${items[index]}") index++ } Source: kotlinlang.org /License: Apache 2.0

Slide 65

Slide 65 text

when expression

Slide 66

Slide 66 text

fun describe(obj: Any): String = when (obj) { 1 -> "One" "Hello" -> "Greeting" is Long -> "Long" !is String -> "Not a string" else -> "Unknown" } Source: kotlinlang.org /License: Apache 2.0

Slide 67

Slide 67 text

Collections

Slide 68

Slide 68 text

val fruits = listOf("banana", "avocado") fruits .filter { it.startsWith("a") } .sortedBy { it } .map { it.toUpperCase() } .forEach { println(it) } Source: kotlinlang.org /License: Apache 2.0

Slide 69

Slide 69 text

Classes

Slide 70

Slide 70 text

class Person { var firstName: String var lastName: String constructor(firstName: String, lastName: String) { this.firstName = firstName this.lastName = lastName } }

Slide 71

Slide 71 text

class Person(var firstName: String, var lastName: String)

Slide 72

Slide 72 text

val person = Person("Hadi", "Tok")

Slide 73

Slide 73 text

Getter setter

Slide 74

Slide 74 text

class Person(var firstName: String, var lastName: String){ var name = "$firstName $lastName" }

Slide 75

Slide 75 text

class Person(var firstName: String, var lastName: String) { var name: String get() = "$firstName $lastName" }

Slide 76

Slide 76 text

class Person(var firstName: String, var lastName: String) { var name: String get() = "$firstName $lastName" set(value) { val array = value.split(" ") firstName = array[0] lastName = array[1] } }

Slide 77

Slide 77 text

Data Classes

Slide 78

Slide 78 text

class Person { String firstName; String lastName; }

Slide 79

Slide 79 text

class Person{ private String firstName; private String lastName; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } }

Slide 80

Slide 80 text

class Person{ private String firstName; private String lastName; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Person person = (Person) o; return Objects.equals(firstName, person.firstName) && Objects.equals(lastName, person.lastName); } @Override public int hashCode() { return Objects.hash(firstName, lastName); } }

Slide 81

Slide 81 text

class Person{ private String firstName; private String lastName; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Person person = (Person) o; return Objects.equals(firstName, person.firstName) && Objects.equals(lastName, person.lastName); } @Override public int hashCode() { return Objects.hash(firstName, lastName); } @Override public String toString() { return "Person{" + "firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + '}'; } }

Slide 82

Slide 82 text

class Person(var firstName: String, var lastName: String)

Slide 83

Slide 83 text

data class Person(var firstName: String, var lastName: String)

Slide 84

Slide 84 text

Sealed classes

Slide 85

Slide 85 text

sealed class Result() data class Success(val value: String) : Result() data class Failure(val e: Exception) : Result()

Slide 86

Slide 86 text

when(expr) { is Success -> println(result.value) is Failure -> println(result.e.message) // the else is not required because covered all the cases }

Slide 87

Slide 87 text

Objects

Slide 88

Slide 88 text

object PersonPrinter { fun printPerson(person: Person) { print(person.toString()) } }

Slide 89

Slide 89 text

object PersonPrinter { fun printPerson(person: Person) { print(person.toString()) } } PersonPrinter.printPerson(Person("Hadi", "Tok"))

Slide 90

Slide 90 text

Inline classes

Slide 91

Slide 91 text

inline class Address(val value: String)

Slide 92

Slide 92 text

inline class Address(val value: String) object AddressPrinter{ fun printAddress(address: Address){ print(address.value) } }

Slide 93

Slide 93 text

Delegated Properties

Slide 94

Slide 94 text

val lazyValue: String by lazy { println("computed!") "Hello" } fun main() { println(lazyValue) println(lazyValue) } Source: kotlinlang.org /License: Apache 2.0

Slide 95

Slide 95 text

Coroutines

Slide 96

Slide 96 text

fun printer(){ GlobalScope.launch { val result = longRunningOperation() println("result is $result") } } suspend fun longRunningOperation(): String { delay(5000L) return "200" } Source: kotlinlang.org /License: Apache 2.0

Slide 97

Slide 97 text

New Vesions of Java

Slide 98

Slide 98 text

What is new with Java? Java 9 ● Private methods in interfaces

Slide 99

Slide 99 text

What is new with Java? Java 10 ● var keyword (type inference)

Slide 100

Slide 100 text

var a = "asd"; System.out.println(a);

Slide 101

Slide 101 text

What is new with Java? Java 11 ● Local Variable Syntax for Lambda Parameters

Slide 102

Slide 102 text

interface LambdaInterface { abstract void doSomething(String x); } LambdaInterface lambdaInterface = ((String x) -> System.out.println(x) );

Slide 103

Slide 103 text

interface LambdaInterface { abstract void doSomething(String x); } LambdaInterface lambdaInterface = ((var x) -> System.out.println(x) );

Slide 104

Slide 104 text

What is new with Java? Java 12

Slide 105

Slide 105 text

What is new with Java? Java 13

Slide 106

Slide 106 text

What is new with Java? Java 14 ● Switch Expressions

Slide 107

Slide 107 text

String result = ""; int number = 0; switch (0) { case 1: case 2: case 3: result = "low"; break; case 4: result = "ok"; break; case 5: case 6: case 7: result = "high"; break; default: result ="unknown"; } System.out.println("result");

Slide 108

Slide 108 text

String result ="" switch (0) { case 1,2,3: result = "low"; break; case 4: result = "ok"; break; case 5,6,7: result = "high"; break; default: result ="unknown"; } System.out.println(result);

Slide 109

Slide 109 text

String result = switch (0) { case 1, 2, 3 -> "low"; case 4 -> "ok"; case 5, 6, 7 -> "high"; default -> "unknown"; }; System.out.println(result);

Slide 110

Slide 110 text

What is new with Java? Java 15 and later ● Text Blocks

Slide 111

Slide 111 text

String html = """

Hello, world

"""; Source: http://openjdk.java.net/jeps/368

Slide 112

Slide 112 text

String html = """

Hello, world

"""; Source: http://openjdk.java.net/jeps/368

Slide 113

Slide 113 text

String html = """

Hello, world

"""; Source: http://openjdk.java.net/jeps/368

Slide 114

Slide 114 text

What is new with Java? Java 15 and later ● Text Blocks ● Pattern Matching for instanceof

Slide 115

Slide 115 text

if(obj instanceof String){ String s = (String) obj; System.out.println(s); }

Slide 116

Slide 116 text

if(obj instanceof String s){ System.out.println(s); }

Slide 117

Slide 117 text

What is new with Java? Java 15 and later ● Text Blocks ● Pattern Matching for instanceof ● Records

Slide 118

Slide 118 text

record Person(String firstName, String lastName) { } Source: http://openjdk.java.net/jeps/368

Slide 119

Slide 119 text

record Person(String firstName, String lastName) { } Person p = new Person("Hadi", "Tok"); Source: http://openjdk.java.net/jeps/368

Slide 120

Slide 120 text

record Person(String firstName, String lastName) { } Person p = new Person("Hadi", "Tok"); System.out.println(p.firstName() + " " + p.lastName()); Source: http://openjdk.java.net/jeps/368

Slide 121

Slide 121 text

Questions? Usefull Links ● Kotlin web site: ○ https://kotlinlang.org/ ● Try Kotlin ○ https://play.kotlinlang.org/ ● KotlinConf 2019: What's New in Java 19: The end of Kotlin? by Jake Wharton : ○ https://www.youtube.com/watch?v=te3OU9fxC8U

Slide 122

Slide 122 text

Thank you! Hadi Tok @hadi_tok Technical Lead @CitizenMe