SlideShare a Scribd company logo
KOTLIN
BETTER JAVA
Dariusz Lorenc
MY KOTLIN
JOURNEY
WHY DO I CARE?
JAVA IS EVOLVING SLOWLY
▸ Java 5 - 2005
▸ for each, autoboxing, generics, better concurrency
▸ Java 6 - 2006
▸ Java 7 - 2011
▸ Java 8 - 2014
▸ lambdas, streams
▸ Java 9 - 2017?
” MOST PEOPLE TALK ABOUT JAVA THE LANGUAGE,
AND THIS MAY SOUND ODD COMING FROM ME, BUT
I COULD HARDLY CARE LESS. AT THE CORE OF THE
JAVA ECOSYSTEM IS THE JVM. “
James Gosling,

Creator of the Java Programming Language
OTHER JVM LANGUAGES
▸ Groovy
▸ dynamic language
▸ Scala
▸ too many features / steeper learning curve
▸ slow compilation time
▸ too much emphasis on FP?
▸ Clojure
▸ dynamic language, dynamically typed
▸ different syntex, too LISPy?
KOTLIN - KEY FEATURES
▸ Statically typed, statically compiled
▸ Built on proven solutions, but simplified
▸ Great Java inter-op
▸ OO at core
▸ FP-friendly
▸ function types
▸ immutability
▸ lets you program in functional style but does not force you
ANY BETTER?
▸ Fixes some basic java problems
▸ Draws inspiration from Effective Java
▸ Provides more modern syntax & concepts
▸ Avoids (java’s) boilerplate
▸ Focuses on pragmatism
SYNTAX
IN 3 SLIDES
VARIABLES / PROPERTIES
var greeting: String
private val hello: String = “hello”
*no static fields/properties
FUNCTIONS
fun greet(name: String): String {
return “hello $name”
}
private fun print(name: String) {
println(greet(name))
}
*no static functions
CLASSES
class Hello : Greeting {
private val greeting: String = “hello”
override fun greet() {
print(greeting)
}
}
WHY DO I CARE
Comparing to Java, Kotlin allows me to write
▸ safer
▸ concise and readable
code, so I can be
‣ more productive
but can still leverage
‣ java’s rich ecosystem
‣ JVM platform
SAFER
JAVA - WHAT COULD POSSIBLY GO WRONG
public class JavaGreeting {
private String greeting;
public JavaGreeting(String greeting) {
this.greeting = greeting;
}
public String getGreeting() {
return this.greeting;
}
}
JMM
CAN TRICK YOU
KOTLIN - FINAL PROPERTIES
class KotlinGreeting(val greeting: String)
JAVA - WHAT COULD POSSIBLY GO WRONG
public class JavaGreeting {
private final String greeting;
public JavaGreeting(String greeting) {
this.greeting = greeting;
}
public int length() {
return this.greeting.length();
}
}
NPE
#1 EXC. IN PROD
KOTLIN - NULL SAFETY
class KotlinGreeting(val greeting: String) {
fun length() = greeting.length
}
class KotlinGreeting(val greeting: String?) {
…
greeting?.length //enforced by compiler
greeting!!.length //enforced by compiler
}
JAVA - WHAT COULD POSSIBLY GO WRONG
public class JavaGreetings {
private final List<String> greetings;
public JavaGreetings(List<String> greetings) {
if (greetings == null)
throw new NullPointerException();
this.greetings = greetings;
}
public void addGreeting(String greeting) {
this.greetings.add(greeting);
}
}
KOTLIN - IMMUTABLE COLLECTIONS
class KotlinGreetings(
private val greetings: List<String>) {
fun addGreeting(greeting: String) {
greetings.add(“hola”) //doesn’t compile
}
}
class KotlinGreetings(
private val greetings: MutableList<String>)
JAVA - WHAT COULD POSSIBLY GO WRONG
interface Greeting { }
public class Hello extends Greeting {
public String say() {
return “hello”;
}
}
if (greeting instanceOf Greeting) {
Hello hello = (Hello) greeting
hello.say()
}
CLASS CAST
#7 EXC. IN PROD
KOTLIN - SMART CAST
if (greeting is Hello) {
greeting.say()
}
JAVA - WHAT COULD POSSIBLY GO WRONG
public String say() { return “hello”; }
say() == “hello”
KOTLIN - REFERENTIAL EQUALITY
fun say() = “hello”
say() == “hello” //true
say() === “hello” //false
say() !== “hello” //true
JAVA - WHAT COULD POSSIBLY GO WRONG
public class JavaGreeting {
private final String greeting;
public JavaGreeting(String greeting) {
this.greeting = greeting;
}
public boolean equals(Object aGreeting) {
//some ide-generated code
}
}
EQUALS-HASHCODE
CONTRACT
KOTLIN - DATA CLASSES
data class KotlinGreeting(val greeting: String)
JAVA - WHAT COULD POSSIBLY GO WRONG
public class JavaGreeting {
private final String greeting;
public JavaGreeting(String greeting) {
this.greeting = greeting;
}
public boolean equals(JavaGreeting aGreeting) {
//some ide-generated code
}
public int hashCode() {
//some ide-generated code
}
}
KOTLIN - OVERRIDE
class KotlinGreeting(val greeting: String) {
//computer says NO!
override fun equals(aGreeting: KotlinGreeting):
Boolean {
…
}
override fun hashCode(): Int {
…
}
}
JAVA - WHAT COULD POSSIBLY GO WRONG
public class JavaGreeting {
protected void sayHi() { … }
protected void sayHello() {
}
}
public class HelloGreeting extends JavaGreeting {
protected void sayHi() {
sayHello();
}
}
sayHi();
FRAGILE
BASE CLASS
KOTLIN - FINAL CLASSES & FUNCTIONS
class KotlinGreeting {
fun sayHi() { … }
fun sayHello() { … }
}
//computer says NO!
class HelloGreeting : KotlinGreeting {
}
OTHER SAFETY FEATURES
▸ Serialisable & inner classes
▸ No statics
▸ Functional code
▸ Safer multithreading
▸ More testable code
CONCISE &
READABLE
CONCISE & NOT READABLE
String one, two, three = two = one = "";
boolean a = false, b = one==two ? two==three : a
EXPRESSION BODY
fun greeting(): String {
return “Hello Kotlin”
}
fun greeting(): String = “Hello Kotlin”
TYPE INFERENCE
val greeting: String = “Hello Kotlin”
val greeting = “Hello Kotlin”
fun greeting(): String = “Hello Kotlin”
fun greeting() = “Hello Kotlin”
CLASS DECLARATION + CONSTRUCTOR + PROPERTIES
class KotlinGreeting(val greeting: String)
public class JavaGreeting {
private final String greeting;
public JavaGreeting(String greeting) {
this.greeting = greeting;
}
public String getGreeting() {
return this.greeting;
}
}
NAMED ARGUMENTS - SAY NO TO BUILDERS
class LangGreeting(
val greeting: String,
val lang: String
)
LangGreeting(greeting = “Hello”, lang = “Kotlin”)
LangGreeting(lang = “Java”, greeting = “bye, bye!”)
DEFAULT VALUES
class LangGreeting(
val greeting = “Hello”,
val lang: String
)
LangGreeting(lang = “Kotlin”)
LangGreeting(lang = “Java”, greeting = “bye, bye!”)
DATA CLASS
data class KotlinGreeting(val greeting: String)
‣ equals()
‣ hashCode()
‣ toString()
‣ copy()
‣ componentN()
DATA CLASSES & IMMUTABILITY
data class LangGreeting(
val greeting: String,
val lang: String
)
val kotlinGreeting = LangGreeting(
greeting = “Hello”,
lang = “Kotlin”
)
val javaGreeting = kotlinGreeting.copy(
lang = “Java”
)
DATA CLASSES & DESTRUCTURING
data class LangGreeting(
val greeting: String,
val lang: String
)
val (greeting, lang) = kotlinGreeting
LAMBDAS
val ints = 1..10
val doubled = ints.map { value -> value * 2 }
val doubled = ints.map { it * 2 }
DELEGATION
interface Greeting {
fun print()
}
class KotlinGreeting(val greeting: String) :
Greeting {
override fun print() { print(greeting) }
}
class DecoratedGreeting(g: Greeting) : Greeting by g
WHEN EXPRESSION
when (x) {
0, 1 -> print("x == 0 or x == 1")
else -> print("otherwise")
}
val y = when (x) {
0, 1 -> true
else -> false
}
CONCISE & READABLE - OTHER FEATURES
▸ Better defaults: final, public?, nested classes are not inner
▸ Extension functions
▸ Default imports
▸ Sealed classes
AND THERE IS MORE …
▸ Coroutines - experimental in Kotlin 1.1
▸ Better Kotlin support in Spring 5
▸ Spek
▸ Kotlin/Native
▸ Kotlin to JavaScript
▸ Gradle Kotlin Script
JAVA INTEROP & GOTCHAS
▸ Using Java libs in Kotlin is straightforward
▸ Using Kotlin libs in Java
▸ Understand how Kotlin compiles to Java
▸ Final classes - spring, mockito …
▸ mockito-kotlin wrapper
▸ kotlin-allopen & spring plugin
▸ `when` is a keyword
▸ “$” is used for string interpolation
REFERENCES
▸ http://kotlinlang.org/docs/reference/
▸ Kotlin in Action [Book]
▸ https://github.com/Kotlin/kotlin-koans
▸ https://try.kotlinlang.org/
▸ https://github.com/KotlinBy/awesome-kotlin
Q&A

More Related Content

PPTX
Introduction to Koltin for Android Part I
Atif AbbAsi
 
PPTX
Kotlin as a Better Java
Garth Gilmour
 
PPTX
Coroutines in Kotlin
Alexey Soshin
 
PPT
The Kotlin Programming Language
intelliyole
 
PDF
A quick and fast intro to Kotlin
XPeppers
 
PDF
1 kotlin vs. java: some java issues addressed in kotlin
Sergey Bandysik
 
PDF
Kotlin for Android Development
Speck&Tech
 
PDF
Introduction to Kotlin coroutines
Roman Elizarov
 
Introduction to Koltin for Android Part I
Atif AbbAsi
 
Kotlin as a Better Java
Garth Gilmour
 
Coroutines in Kotlin
Alexey Soshin
 
The Kotlin Programming Language
intelliyole
 
A quick and fast intro to Kotlin
XPeppers
 
1 kotlin vs. java: some java issues addressed in kotlin
Sergey Bandysik
 
Kotlin for Android Development
Speck&Tech
 
Introduction to Kotlin coroutines
Roman Elizarov
 

What's hot (20)

PPTX
Kotlin
Rory Preddy
 
PDF
Kotlin vs Java | Edureka
Edureka!
 
PPTX
Intro to kotlin
Tomislav Homan
 
PDF
Deep drive into rust programming language
Vigneshwer Dhinakaran
 
PPTX
Java 8 Lambda and Streams
Venkata Naga Ravi
 
PDF
Introduction to kotlin for android app development gdg ahmedabad dev fest 2017
Hardik Trivedi
 
PDF
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Edureka!
 
PDF
Lambda and Stream Master class - part 1
José Paumard
 
PDF
Introduction to kotlin
NAVER Engineering
 
PDF
Algebraic Data Types for Data Oriented Programming - From Haskell and Scala t...
Philip Schwarz
 
PDF
Java 8 lambda expressions
Logan Chien
 
PPT
C++ oop
Sunil OS
 
ODP
Java 9 Features
NexThoughts Technologies
 
PPT
Java Basics
Sunil OS
 
PDF
Introduction to kotlin coroutines
NAVER Engineering
 
PPTX
Android with kotlin course
Abdul Rahman Masri Attal
 
PPTX
Kotlin on android
Kurt Renzo Acosta
 
PDF
Jetpack Compose a new way to implement UI on Android
Nelson Glauber Leal
 
PPTX
Python Programming Essentials - M27 - Logging module
P3 InfoTech Solutions Pvt. Ltd.
 
Kotlin
Rory Preddy
 
Kotlin vs Java | Edureka
Edureka!
 
Intro to kotlin
Tomislav Homan
 
Deep drive into rust programming language
Vigneshwer Dhinakaran
 
Java 8 Lambda and Streams
Venkata Naga Ravi
 
Introduction to kotlin for android app development gdg ahmedabad dev fest 2017
Hardik Trivedi
 
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Edureka!
 
Lambda and Stream Master class - part 1
José Paumard
 
Introduction to kotlin
NAVER Engineering
 
Algebraic Data Types for Data Oriented Programming - From Haskell and Scala t...
Philip Schwarz
 
Java 8 lambda expressions
Logan Chien
 
C++ oop
Sunil OS
 
Java 9 Features
NexThoughts Technologies
 
Java Basics
Sunil OS
 
Introduction to kotlin coroutines
NAVER Engineering
 
Android with kotlin course
Abdul Rahman Masri Attal
 
Kotlin on android
Kurt Renzo Acosta
 
Jetpack Compose a new way to implement UI on Android
Nelson Glauber Leal
 
Python Programming Essentials - M27 - Logging module
P3 InfoTech Solutions Pvt. Ltd.
 
Ad

Similar to Kotlin - Better Java (20)

PDF
Having Fun with Kotlin Android - DILo Surabaya
DILo Surabaya
 
PDF
Kotlin: a better Java
Nils Breunese
 
PDF
2022 May - Shoulders of Giants - Amsterdam - Kotlin Dev Day.pdf
Andrey Breslav
 
PDF
Kotlin, smarter development for the jvm
Arnaud Giuliani
 
PDF
Be More Productive with Kotlin
Brandon Wever
 
PDF
Privet Kotlin (Windy City DevFest)
Cody Engel
 
PDF
Kotlin intro
Elifarley Cruz
 
PDF
A short introduction to the Kotlin language for Java developers
Antonis Lilis
 
PPTX
Kotlin – the future of android
DJ Rausch
 
PDF
Kotlin: Challenges in JVM language design
Andrey Breslav
 
PDF
Is this Swift for Android? A short introduction to the Kotlin language
Antonis Lilis
 
PDF
Intro to Kotlin
Magda Miu
 
PDF
Taking Kotlin to production, Seriously
Haim Yadid
 
PDF
JDD 2017: Kotlin for Java developers (Tomasz Kleszczyński)
PROIDEA
 
PDF
Kotlin: A pragmatic language by JetBrains
Jigar Gosar
 
PDF
Java To Kotlin A Refactoring Guidebook 1st Edition Duncan Mcgregor
daiziyuleth25
 
PDF
Kotlin: forse è la volta buona (Trento)
Davide Cerbo
 
PPTX
Introduction to Kotlin Language and its application to Android platform
EastBanc Tachnologies
 
PDF
Dear Kotliners - Java Developers are Humans too
Vivek Chanddru
 
PDF
Exploring Kotlin
Johan Haleby
 
Having Fun with Kotlin Android - DILo Surabaya
DILo Surabaya
 
Kotlin: a better Java
Nils Breunese
 
2022 May - Shoulders of Giants - Amsterdam - Kotlin Dev Day.pdf
Andrey Breslav
 
Kotlin, smarter development for the jvm
Arnaud Giuliani
 
Be More Productive with Kotlin
Brandon Wever
 
Privet Kotlin (Windy City DevFest)
Cody Engel
 
Kotlin intro
Elifarley Cruz
 
A short introduction to the Kotlin language for Java developers
Antonis Lilis
 
Kotlin – the future of android
DJ Rausch
 
Kotlin: Challenges in JVM language design
Andrey Breslav
 
Is this Swift for Android? A short introduction to the Kotlin language
Antonis Lilis
 
Intro to Kotlin
Magda Miu
 
Taking Kotlin to production, Seriously
Haim Yadid
 
JDD 2017: Kotlin for Java developers (Tomasz Kleszczyński)
PROIDEA
 
Kotlin: A pragmatic language by JetBrains
Jigar Gosar
 
Java To Kotlin A Refactoring Guidebook 1st Edition Duncan Mcgregor
daiziyuleth25
 
Kotlin: forse è la volta buona (Trento)
Davide Cerbo
 
Introduction to Kotlin Language and its application to Android platform
EastBanc Tachnologies
 
Dear Kotliners - Java Developers are Humans too
Vivek Chanddru
 
Exploring Kotlin
Johan Haleby
 
Ad

Recently uploaded (20)

PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
Software Development Company | KodekX
KodekX
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
PDF
REPORT: Heating appliances market in Poland 2024
SPIUG
 
PDF
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
PDF
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
Beyond Automation: The Role of IoT Sensor Integration in Next-Gen Industries
Rejig Digital
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Doc9.....................................
SofiaCollazos
 
Software Development Company | KodekX
KodekX
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
REPORT: Heating appliances market in Poland 2024
SPIUG
 
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
Beyond Automation: The Role of IoT Sensor Integration in Next-Gen Industries
Rejig Digital
 

Kotlin - Better Java

  • 3. WHY DO I CARE?
  • 4. JAVA IS EVOLVING SLOWLY ▸ Java 5 - 2005 ▸ for each, autoboxing, generics, better concurrency ▸ Java 6 - 2006 ▸ Java 7 - 2011 ▸ Java 8 - 2014 ▸ lambdas, streams ▸ Java 9 - 2017?
  • 5. ” MOST PEOPLE TALK ABOUT JAVA THE LANGUAGE, AND THIS MAY SOUND ODD COMING FROM ME, BUT I COULD HARDLY CARE LESS. AT THE CORE OF THE JAVA ECOSYSTEM IS THE JVM. “ James Gosling,
 Creator of the Java Programming Language
  • 6. OTHER JVM LANGUAGES ▸ Groovy ▸ dynamic language ▸ Scala ▸ too many features / steeper learning curve ▸ slow compilation time ▸ too much emphasis on FP? ▸ Clojure ▸ dynamic language, dynamically typed ▸ different syntex, too LISPy?
  • 7. KOTLIN - KEY FEATURES ▸ Statically typed, statically compiled ▸ Built on proven solutions, but simplified ▸ Great Java inter-op ▸ OO at core ▸ FP-friendly ▸ function types ▸ immutability ▸ lets you program in functional style but does not force you
  • 8. ANY BETTER? ▸ Fixes some basic java problems ▸ Draws inspiration from Effective Java ▸ Provides more modern syntax & concepts ▸ Avoids (java’s) boilerplate ▸ Focuses on pragmatism
  • 10. VARIABLES / PROPERTIES var greeting: String private val hello: String = “hello” *no static fields/properties
  • 11. FUNCTIONS fun greet(name: String): String { return “hello $name” } private fun print(name: String) { println(greet(name)) } *no static functions
  • 12. CLASSES class Hello : Greeting { private val greeting: String = “hello” override fun greet() { print(greeting) } }
  • 13. WHY DO I CARE Comparing to Java, Kotlin allows me to write ▸ safer ▸ concise and readable code, so I can be ‣ more productive but can still leverage ‣ java’s rich ecosystem ‣ JVM platform
  • 14. SAFER
  • 15. JAVA - WHAT COULD POSSIBLY GO WRONG public class JavaGreeting { private String greeting; public JavaGreeting(String greeting) { this.greeting = greeting; } public String getGreeting() { return this.greeting; } }
  • 17. KOTLIN - FINAL PROPERTIES class KotlinGreeting(val greeting: String)
  • 18. JAVA - WHAT COULD POSSIBLY GO WRONG public class JavaGreeting { private final String greeting; public JavaGreeting(String greeting) { this.greeting = greeting; } public int length() { return this.greeting.length(); } }
  • 20. KOTLIN - NULL SAFETY class KotlinGreeting(val greeting: String) { fun length() = greeting.length } class KotlinGreeting(val greeting: String?) { … greeting?.length //enforced by compiler greeting!!.length //enforced by compiler }
  • 21. JAVA - WHAT COULD POSSIBLY GO WRONG public class JavaGreetings { private final List<String> greetings; public JavaGreetings(List<String> greetings) { if (greetings == null) throw new NullPointerException(); this.greetings = greetings; } public void addGreeting(String greeting) { this.greetings.add(greeting); } }
  • 22. KOTLIN - IMMUTABLE COLLECTIONS class KotlinGreetings( private val greetings: List<String>) { fun addGreeting(greeting: String) { greetings.add(“hola”) //doesn’t compile } } class KotlinGreetings( private val greetings: MutableList<String>)
  • 23. JAVA - WHAT COULD POSSIBLY GO WRONG interface Greeting { } public class Hello extends Greeting { public String say() { return “hello”; } } if (greeting instanceOf Greeting) { Hello hello = (Hello) greeting hello.say() }
  • 25. KOTLIN - SMART CAST if (greeting is Hello) { greeting.say() }
  • 26. JAVA - WHAT COULD POSSIBLY GO WRONG public String say() { return “hello”; } say() == “hello”
  • 27. KOTLIN - REFERENTIAL EQUALITY fun say() = “hello” say() == “hello” //true say() === “hello” //false say() !== “hello” //true
  • 28. JAVA - WHAT COULD POSSIBLY GO WRONG public class JavaGreeting { private final String greeting; public JavaGreeting(String greeting) { this.greeting = greeting; } public boolean equals(Object aGreeting) { //some ide-generated code } }
  • 30. KOTLIN - DATA CLASSES data class KotlinGreeting(val greeting: String)
  • 31. JAVA - WHAT COULD POSSIBLY GO WRONG public class JavaGreeting { private final String greeting; public JavaGreeting(String greeting) { this.greeting = greeting; } public boolean equals(JavaGreeting aGreeting) { //some ide-generated code } public int hashCode() { //some ide-generated code } }
  • 32. KOTLIN - OVERRIDE class KotlinGreeting(val greeting: String) { //computer says NO! override fun equals(aGreeting: KotlinGreeting): Boolean { … } override fun hashCode(): Int { … } }
  • 33. JAVA - WHAT COULD POSSIBLY GO WRONG public class JavaGreeting { protected void sayHi() { … } protected void sayHello() { } } public class HelloGreeting extends JavaGreeting { protected void sayHi() { sayHello(); } } sayHi();
  • 35. KOTLIN - FINAL CLASSES & FUNCTIONS class KotlinGreeting { fun sayHi() { … } fun sayHello() { … } } //computer says NO! class HelloGreeting : KotlinGreeting { }
  • 36. OTHER SAFETY FEATURES ▸ Serialisable & inner classes ▸ No statics ▸ Functional code ▸ Safer multithreading ▸ More testable code
  • 38. CONCISE & NOT READABLE String one, two, three = two = one = ""; boolean a = false, b = one==two ? two==three : a
  • 39. EXPRESSION BODY fun greeting(): String { return “Hello Kotlin” } fun greeting(): String = “Hello Kotlin”
  • 40. TYPE INFERENCE val greeting: String = “Hello Kotlin” val greeting = “Hello Kotlin” fun greeting(): String = “Hello Kotlin” fun greeting() = “Hello Kotlin”
  • 41. CLASS DECLARATION + CONSTRUCTOR + PROPERTIES class KotlinGreeting(val greeting: String) public class JavaGreeting { private final String greeting; public JavaGreeting(String greeting) { this.greeting = greeting; } public String getGreeting() { return this.greeting; } }
  • 42. NAMED ARGUMENTS - SAY NO TO BUILDERS class LangGreeting( val greeting: String, val lang: String ) LangGreeting(greeting = “Hello”, lang = “Kotlin”) LangGreeting(lang = “Java”, greeting = “bye, bye!”)
  • 43. DEFAULT VALUES class LangGreeting( val greeting = “Hello”, val lang: String ) LangGreeting(lang = “Kotlin”) LangGreeting(lang = “Java”, greeting = “bye, bye!”)
  • 44. DATA CLASS data class KotlinGreeting(val greeting: String) ‣ equals() ‣ hashCode() ‣ toString() ‣ copy() ‣ componentN()
  • 45. DATA CLASSES & IMMUTABILITY data class LangGreeting( val greeting: String, val lang: String ) val kotlinGreeting = LangGreeting( greeting = “Hello”, lang = “Kotlin” ) val javaGreeting = kotlinGreeting.copy( lang = “Java” )
  • 46. DATA CLASSES & DESTRUCTURING data class LangGreeting( val greeting: String, val lang: String ) val (greeting, lang) = kotlinGreeting
  • 47. LAMBDAS val ints = 1..10 val doubled = ints.map { value -> value * 2 } val doubled = ints.map { it * 2 }
  • 48. DELEGATION interface Greeting { fun print() } class KotlinGreeting(val greeting: String) : Greeting { override fun print() { print(greeting) } } class DecoratedGreeting(g: Greeting) : Greeting by g
  • 49. WHEN EXPRESSION when (x) { 0, 1 -> print("x == 0 or x == 1") else -> print("otherwise") } val y = when (x) { 0, 1 -> true else -> false }
  • 50. CONCISE & READABLE - OTHER FEATURES ▸ Better defaults: final, public?, nested classes are not inner ▸ Extension functions ▸ Default imports ▸ Sealed classes
  • 51. AND THERE IS MORE … ▸ Coroutines - experimental in Kotlin 1.1 ▸ Better Kotlin support in Spring 5 ▸ Spek ▸ Kotlin/Native ▸ Kotlin to JavaScript ▸ Gradle Kotlin Script
  • 52. JAVA INTEROP & GOTCHAS ▸ Using Java libs in Kotlin is straightforward ▸ Using Kotlin libs in Java ▸ Understand how Kotlin compiles to Java ▸ Final classes - spring, mockito … ▸ mockito-kotlin wrapper ▸ kotlin-allopen & spring plugin ▸ `when` is a keyword ▸ “$” is used for string interpolation
  • 53. REFERENCES ▸ http://kotlinlang.org/docs/reference/ ▸ Kotlin in Action [Book] ▸ https://github.com/Kotlin/kotlin-koans ▸ https://try.kotlinlang.org/ ▸ https://github.com/KotlinBy/awesome-kotlin
  • 54. Q&A