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

What's hot

Kotlin
KotlinKotlin
Kotlin
Rory Preddy
 
Kotlin vs Java | Edureka
Kotlin vs Java | EdurekaKotlin vs Java | Edureka
Kotlin vs Java | Edureka
Edureka!
 
Introduction to Coroutines @ KotlinConf 2017
Introduction to Coroutines @ KotlinConf 2017Introduction to Coroutines @ KotlinConf 2017
Introduction to Coroutines @ KotlinConf 2017
Roman Elizarov
 
Kotlin presentation
Kotlin presentation Kotlin presentation
Kotlin presentation
MobileAcademy
 
Intro to kotlin
Intro to kotlinIntro to kotlin
Intro to kotlin
Tomislav Homan
 
Introduction to kotlin
Introduction to kotlinIntroduction to kotlin
Introduction to kotlin
NAVER Engineering
 
Idiomatic Kotlin
Idiomatic KotlinIdiomatic Kotlin
Idiomatic Kotlin
intelliyole
 
Clean coding-practices
Clean coding-practicesClean coding-practices
Clean coding-practices
John Ferguson Smart Limited
 
Android Development with Kotlin course
Android Development  with Kotlin courseAndroid Development  with Kotlin course
Android Development with Kotlin course
GoogleDevelopersLeba
 
Android with kotlin course
Android with kotlin courseAndroid with kotlin course
Android with kotlin course
Abdul Rahman Masri Attal
 
Kotlin Language powerpoint show file
Kotlin Language powerpoint show fileKotlin Language powerpoint show file
Kotlin Language powerpoint show file
Saurabh Tripathi
 
Introduction to Koltin for Android Part I
Introduction to Koltin for Android Part I Introduction to Koltin for Android Part I
Introduction to Koltin for Android Part I
Atif AbbAsi
 
Introduction to kotlin coroutines
Introduction to kotlin coroutinesIntroduction to kotlin coroutines
Introduction to kotlin coroutines
NAVER Engineering
 
Introduction to Kotlin Language and its application to Android platform
Introduction to Kotlin Language and its application to Android platformIntroduction to Kotlin Language and its application to Android platform
Introduction to Kotlin Language and its application to Android platform
EastBanc Tachnologies
 
Jetpack compose
Jetpack composeJetpack compose
Jetpack compose
LutasLin
 
Introduction to Kotlin coroutines
Introduction to Kotlin coroutinesIntroduction to Kotlin coroutines
Introduction to Kotlin coroutines
Roman Elizarov
 
炎炎夏日學 Android 課程 - Part1: Kotlin 語法介紹
炎炎夏日學 Android 課程 -  Part1: Kotlin 語法介紹炎炎夏日學 Android 課程 -  Part1: Kotlin 語法介紹
炎炎夏日學 Android 課程 - Part1: Kotlin 語法介紹
Johnny Sung
 
Deep dive into Coroutines on JVM @ KotlinConf 2017
Deep dive into Coroutines on JVM @ KotlinConf 2017Deep dive into Coroutines on JVM @ KotlinConf 2017
Deep dive into Coroutines on JVM @ KotlinConf 2017
Roman Elizarov
 
Kotlin Coroutines Reloaded
Kotlin Coroutines ReloadedKotlin Coroutines Reloaded
Kotlin Coroutines Reloaded
Roman Elizarov
 
Golang - Overview of Go (golang) Language
Golang - Overview of Go (golang) LanguageGolang - Overview of Go (golang) Language
Golang - Overview of Go (golang) Language
Aniruddha Chakrabarti
 

What's hot (20)

Kotlin
KotlinKotlin
Kotlin
 
Kotlin vs Java | Edureka
Kotlin vs Java | EdurekaKotlin vs Java | Edureka
Kotlin vs Java | Edureka
 
Introduction to Coroutines @ KotlinConf 2017
Introduction to Coroutines @ KotlinConf 2017Introduction to Coroutines @ KotlinConf 2017
Introduction to Coroutines @ KotlinConf 2017
 
Kotlin presentation
Kotlin presentation Kotlin presentation
Kotlin presentation
 
Intro to kotlin
Intro to kotlinIntro to kotlin
Intro to kotlin
 
Introduction to kotlin
Introduction to kotlinIntroduction to kotlin
Introduction to kotlin
 
Idiomatic Kotlin
Idiomatic KotlinIdiomatic Kotlin
Idiomatic Kotlin
 
Clean coding-practices
Clean coding-practicesClean coding-practices
Clean coding-practices
 
Android Development with Kotlin course
Android Development  with Kotlin courseAndroid Development  with Kotlin course
Android Development with Kotlin course
 
Android with kotlin course
Android with kotlin courseAndroid with kotlin course
Android with kotlin course
 
Kotlin Language powerpoint show file
Kotlin Language powerpoint show fileKotlin Language powerpoint show file
Kotlin Language powerpoint show file
 
Introduction to Koltin for Android Part I
Introduction to Koltin for Android Part I Introduction to Koltin for Android Part I
Introduction to Koltin for Android Part I
 
Introduction to kotlin coroutines
Introduction to kotlin coroutinesIntroduction to kotlin coroutines
Introduction to kotlin coroutines
 
Introduction to Kotlin Language and its application to Android platform
Introduction to Kotlin Language and its application to Android platformIntroduction to Kotlin Language and its application to Android platform
Introduction to Kotlin Language and its application to Android platform
 
Jetpack compose
Jetpack composeJetpack compose
Jetpack compose
 
Introduction to Kotlin coroutines
Introduction to Kotlin coroutinesIntroduction to Kotlin coroutines
Introduction to Kotlin coroutines
 
炎炎夏日學 Android 課程 - Part1: Kotlin 語法介紹
炎炎夏日學 Android 課程 -  Part1: Kotlin 語法介紹炎炎夏日學 Android 課程 -  Part1: Kotlin 語法介紹
炎炎夏日學 Android 課程 - Part1: Kotlin 語法介紹
 
Deep dive into Coroutines on JVM @ KotlinConf 2017
Deep dive into Coroutines on JVM @ KotlinConf 2017Deep dive into Coroutines on JVM @ KotlinConf 2017
Deep dive into Coroutines on JVM @ KotlinConf 2017
 
Kotlin Coroutines Reloaded
Kotlin Coroutines ReloadedKotlin Coroutines Reloaded
Kotlin Coroutines Reloaded
 
Golang - Overview of Go (golang) Language
Golang - Overview of Go (golang) LanguageGolang - Overview of Go (golang) Language
Golang - Overview of Go (golang) Language
 

Similar to Kotlin - Better Java

Polyglot Programming @ Jax.de 2010
Polyglot Programming @ Jax.de 2010Polyglot Programming @ Jax.de 2010
Polyglot Programming @ Jax.de 2010
Andres Almiray
 
Polyglot Programming in the JVM
Polyglot Programming in the JVMPolyglot Programming in the JVM
Polyglot Programming in the JVM
Andres Almiray
 
Having Fun with Kotlin Android - DILo Surabaya
Having Fun with Kotlin Android - DILo SurabayaHaving Fun with Kotlin Android - DILo Surabaya
Having Fun with Kotlin Android - DILo Surabaya
DILo Surabaya
 
Polyglot Programming in the JVM - 33rd Degree
Polyglot Programming in the JVM - 33rd DegreePolyglot Programming in the JVM - 33rd Degree
Polyglot Programming in the JVM - 33rd Degree
Andres Almiray
 
JVM languages "flame wars"
JVM languages "flame wars"JVM languages "flame wars"
JVM languages "flame wars"
Gal Marder
 
Kotlin Language Features - A Java comparison
Kotlin Language Features - A Java comparisonKotlin Language Features - A Java comparison
Kotlin Language Features - A Java comparison
Ed Austin
 
Polyglot Programming in the JVM - Øredev
Polyglot Programming in the JVM - ØredevPolyglot Programming in the JVM - Øredev
Polyglot Programming in the JVM - Øredev
Andres Almiray
 
Intro to Kotlin
Intro to KotlinIntro to Kotlin
Intro to Kotlin
Magda Miu
 
Kotlin for Android Developers - 1
Kotlin for Android Developers - 1Kotlin for Android Developers - 1
Kotlin for Android Developers - 1
Mohamed Nabil, MSc.
 
Groovy a Scripting Language for Java
Groovy a Scripting Language for JavaGroovy a Scripting Language for Java
Groovy a Scripting Language for Java
Charles Anderson
 
Develop your next app with kotlin @ AndroidMakersFr 2017
Develop your next app with kotlin @ AndroidMakersFr 2017Develop your next app with kotlin @ AndroidMakersFr 2017
Develop your next app with kotlin @ AndroidMakersFr 2017
Arnaud Giuliani
 
Oscon Java Testing on the Fast Lane
Oscon Java Testing on the Fast LaneOscon Java Testing on the Fast Lane
Oscon Java Testing on the Fast Lane
Andres Almiray
 
Programming with Kotlin
Programming with KotlinProgramming with Kotlin
Programming with Kotlin
David Gassner
 
GTAC Boosting your Testing Productivity with Groovy
GTAC Boosting your Testing Productivity with GroovyGTAC Boosting your Testing Productivity with Groovy
GTAC Boosting your Testing Productivity with Groovy
Andres Almiray
 
Dear Kotliners - Java Developers are Humans too
Dear Kotliners - Java Developers are Humans tooDear Kotliners - Java Developers are Humans too
Dear Kotliners - Java Developers are Humans too
Vivek Chanddru
 
Kotlin – the future of android
Kotlin – the future of androidKotlin – the future of android
Kotlin – the future of android
DJ Rausch
 
Introduction To Scala
Introduction To ScalaIntroduction To Scala
Introduction To Scala
Peter Maas
 
Kotlin, smarter development for the jvm
Kotlin, smarter development for the jvmKotlin, smarter development for the jvm
Kotlin, smarter development for the jvm
Arnaud Giuliani
 
Javaone2008 Bof 5101 Groovytesting
Javaone2008 Bof 5101 GroovytestingJavaone2008 Bof 5101 Groovytesting
Javaone2008 Bof 5101 Groovytesting
Andres Almiray
 
Boosting Your Testing Productivity with Groovy
Boosting Your Testing Productivity with GroovyBoosting Your Testing Productivity with Groovy
Boosting Your Testing Productivity with Groovy
James Williams
 

Similar to Kotlin - Better Java (20)

Polyglot Programming @ Jax.de 2010
Polyglot Programming @ Jax.de 2010Polyglot Programming @ Jax.de 2010
Polyglot Programming @ Jax.de 2010
 
Polyglot Programming in the JVM
Polyglot Programming in the JVMPolyglot Programming in the JVM
Polyglot Programming in the JVM
 
Having Fun with Kotlin Android - DILo Surabaya
Having Fun with Kotlin Android - DILo SurabayaHaving Fun with Kotlin Android - DILo Surabaya
Having Fun with Kotlin Android - DILo Surabaya
 
Polyglot Programming in the JVM - 33rd Degree
Polyglot Programming in the JVM - 33rd DegreePolyglot Programming in the JVM - 33rd Degree
Polyglot Programming in the JVM - 33rd Degree
 
JVM languages "flame wars"
JVM languages "flame wars"JVM languages "flame wars"
JVM languages "flame wars"
 
Kotlin Language Features - A Java comparison
Kotlin Language Features - A Java comparisonKotlin Language Features - A Java comparison
Kotlin Language Features - A Java comparison
 
Polyglot Programming in the JVM - Øredev
Polyglot Programming in the JVM - ØredevPolyglot Programming in the JVM - Øredev
Polyglot Programming in the JVM - Øredev
 
Intro to Kotlin
Intro to KotlinIntro to Kotlin
Intro to Kotlin
 
Kotlin for Android Developers - 1
Kotlin for Android Developers - 1Kotlin for Android Developers - 1
Kotlin for Android Developers - 1
 
Groovy a Scripting Language for Java
Groovy a Scripting Language for JavaGroovy a Scripting Language for Java
Groovy a Scripting Language for Java
 
Develop your next app with kotlin @ AndroidMakersFr 2017
Develop your next app with kotlin @ AndroidMakersFr 2017Develop your next app with kotlin @ AndroidMakersFr 2017
Develop your next app with kotlin @ AndroidMakersFr 2017
 
Oscon Java Testing on the Fast Lane
Oscon Java Testing on the Fast LaneOscon Java Testing on the Fast Lane
Oscon Java Testing on the Fast Lane
 
Programming with Kotlin
Programming with KotlinProgramming with Kotlin
Programming with Kotlin
 
GTAC Boosting your Testing Productivity with Groovy
GTAC Boosting your Testing Productivity with GroovyGTAC Boosting your Testing Productivity with Groovy
GTAC Boosting your Testing Productivity with Groovy
 
Dear Kotliners - Java Developers are Humans too
Dear Kotliners - Java Developers are Humans tooDear Kotliners - Java Developers are Humans too
Dear Kotliners - Java Developers are Humans too
 
Kotlin – the future of android
Kotlin – the future of androidKotlin – the future of android
Kotlin – the future of android
 
Introduction To Scala
Introduction To ScalaIntroduction To Scala
Introduction To Scala
 
Kotlin, smarter development for the jvm
Kotlin, smarter development for the jvmKotlin, smarter development for the jvm
Kotlin, smarter development for the jvm
 
Javaone2008 Bof 5101 Groovytesting
Javaone2008 Bof 5101 GroovytestingJavaone2008 Bof 5101 Groovytesting
Javaone2008 Bof 5101 Groovytesting
 
Boosting Your Testing Productivity with Groovy
Boosting Your Testing Productivity with GroovyBoosting Your Testing Productivity with Groovy
Boosting Your Testing Productivity with Groovy
 

Recently uploaded

TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
saastr
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
ssuserfac0301
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Tosin Akinosho
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
DanBrown980551
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
akankshawande
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Alpen-Adria-Universität
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
Jakub Marek
 
Webinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data WarehouseWebinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data Warehouse
Federico Razzoli
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 

Recently uploaded (20)

TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
 
Webinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data WarehouseWebinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data Warehouse
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 

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