SlideShare a Scribd company logo
NEW BEST FRIENDS:
Kotlin’s Interoperability with Java
Presented by: David Greenhalgh
TODAY’S FORMAT
Housekeeping/Intro
Kotlin Talk by David
Live Q&A
Lunch & Learn / Book Giveaway
BIG NERD RANCH
Travel to your office Discuss Kotlin
Enjoy lunch!
BUILD BRILLIANCE
BIG NERD RANCH
• Immersive BOOTCAMPS
• Onsite TEAM TRAINING
• THE FRONTIER subscription
• Digital PRODUCT
DEVELOPMENT
4
BIG NERD RANCH
BIG NERD RANCH
BIG NERD RANCH
Travel to your office Discuss Kotlin
Enjoy lunch!
How Kotlin Interoperates with Java
Why Kotlin?
• Concise
• Pragmatic
• Flexible
• Type-safe
Why Kotlin?
• Concise
• Pragmatic
Java
person.getName();
Kotlin
person.name
Why Kotlin?
• Type-safe
Objects may not hold null values unless declared using a nullable type
String may not be null
String? may be null
Why Kotlin?
• Full interoperability with Java
Interoperability
Kotlin code and Java code works together directly
• Call Java methods from Kotlin
• Call Kotlin functions from Java
The Kotlin Compilation Process
Kotlin compiles down to Java bytecode
JVM Bytecode
Decompiled Java Bytecode
Interoperability
Kotlin code and Java code works together directly
• Leverage legacy Java frameworks (e.g. Android)
• Gradually transition your codebase from Java to Kotlin
An Example
• Calling a Java method from Kotlin
• Nullability and platform types
• Accessing Java fields from Kotlin
• Type mapping
• Kotlin file anatomy
A Java Method
public class Jhava {
public String utterGreeting() {
return "BLARGH";
}
}
Calling a Java Method from Kotlin
fun main(args: Array<String>) {
val adversary = Jhava()
println(adversary.utterGreeting())
}
Another Java Method
public class Jhava {
public String utterGreeting() {
return "BLARGH";
}
public String determineFriendshipLevel() {
return null;
}
}
Platform Type
NullPointerException in Kotlin
fun main(args: Array<String>) {
...
val friendshipLevel = adversary.determineFriendshipLevel()
println(friendshipLevel.toLowerCase())
}
Nullability Annotations
public class Jhava {
@NotNull
public String utterGreeting() {
return "BLARGH";
}
@Nullable
public String determineFriendshipLevel() {
return null;
}
}
IntelliJ Nullable Type Awareness
Java Field with Getter and Setter
public class Jhava {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Property Reference from Kotlin
fun main(args: Array<String>) {
Jhava().name
}
Type Mapping
Some Kotlin types map directly to Java types
• String to String
Type Mapping
Other Kotlin types do not map directly to Java types
• Kotlin's Int to Java's int
Primitive Types
• Java represents basic data types using "primitve types" (non-objects)
• Kotlin represents all data using objects
Integers in Kotlin
fun main(args: Array<String>) {
val adversary = Jhava()
val adversaryHitPoints: Int = adversary.hitPoints
adversaryHitPoints.dec()
println(adversaryHitPoints.javaClass)
}
>>> int
Kotlin File Anatomy
• Java files contain exactly one class
• Kotlin files may contain classes, functions, and variables at top-level
• How can you call standalone Kotlin functions from Java?
A Standalone Kotlin Function (Hero.kt)
fun main(args: Array<String>) {
...
}
fun makeProclamation() = "Greetings, beast!"
Calling a File-level Function from Java
public static void main(String[] args) {
System.out.println(HeroKt.makeProclamation());
}
Customizing Class Name (Hero.kt)
@file:JvmName("Hero")
fun main(args: Array<String>) {
...
}
fun makeProclamation() = "Greetings, beast!"
Using the Customized Class Name
public static void main(String[] args) {
System.out.println(Hero.makeProclamation());
}
Customizing Compiled Java Code
JVM annotations exist to customize the Java code that is compiled from
your Kotlin source
• @JvmName
• @JvmOverloads
• @JvmStatic
• @JvmField
Refrain: Your Upgrade Path
Kotlin compiles down to Java bytecode
Risks of Sticking with Java
• Slow moving language development
• Less type-safe
• Less concise
• Less interest from developers
Next Steps
• Start a new project in Kotlin
• Upgrade selectively
• Acquire Kotlin resources

More Related Content

What's hot

04 Java Language And OOP Part IV
04 Java Language And OOP Part IV04 Java Language And OOP Part IV
04 Java Language And OOP Part IV
Hari Christian
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Nochiketa Chakraborty
 
05 Java Language And OOP Part V
05 Java Language And OOP Part V05 Java Language And OOP Part V
05 Java Language And OOP Part V
Hari Christian
 
Paca oops slid
Paca oops slidPaca oops slid
Paca oops slid
pacatarpit
 
Ruby
RubyRuby
To Lombok or not to Lombok | J-Fall 2019
To Lombok or not to Lombok | J-Fall 2019To Lombok or not to Lombok | J-Fall 2019
To Lombok or not to Lombok | J-Fall 2019
Kaya Weers
 
JDD 2017: Kotlin for Java developers (Tomasz Kleszczyński)
JDD 2017: Kotlin for Java developers (Tomasz Kleszczyński)JDD 2017: Kotlin for Java developers (Tomasz Kleszczyński)
JDD 2017: Kotlin for Java developers (Tomasz Kleszczyński)
PROIDEA
 
Data classes in kotlin by Naveed
Data classes in kotlin by NaveedData classes in kotlin by Naveed
Java essentials for hadoop
Java essentials for hadoopJava essentials for hadoop
Java essentials for hadoop
Seo Gyansha
 
From DOT to Dotty
From DOT to DottyFrom DOT to Dotty
From DOT to Dotty
Martin Odersky
 
Oops in java
Oops in javaOops in java
06 Java Language And OOP Part VI
06 Java Language And OOP Part VI06 Java Language And OOP Part VI
06 Java Language And OOP Part VI
Hari Christian
 
Object-Oriented Programming Concepts
Object-Oriented Programming ConceptsObject-Oriented Programming Concepts
Object-Oriented Programming Concepts
Kwangshin Oh
 

What's hot (13)

04 Java Language And OOP Part IV
04 Java Language And OOP Part IV04 Java Language And OOP Part IV
04 Java Language And OOP Part IV
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
05 Java Language And OOP Part V
05 Java Language And OOP Part V05 Java Language And OOP Part V
05 Java Language And OOP Part V
 
Paca oops slid
Paca oops slidPaca oops slid
Paca oops slid
 
Ruby
RubyRuby
Ruby
 
To Lombok or not to Lombok | J-Fall 2019
To Lombok or not to Lombok | J-Fall 2019To Lombok or not to Lombok | J-Fall 2019
To Lombok or not to Lombok | J-Fall 2019
 
JDD 2017: Kotlin for Java developers (Tomasz Kleszczyński)
JDD 2017: Kotlin for Java developers (Tomasz Kleszczyński)JDD 2017: Kotlin for Java developers (Tomasz Kleszczyński)
JDD 2017: Kotlin for Java developers (Tomasz Kleszczyński)
 
Data classes in kotlin by Naveed
Data classes in kotlin by NaveedData classes in kotlin by Naveed
Data classes in kotlin by Naveed
 
Java essentials for hadoop
Java essentials for hadoopJava essentials for hadoop
Java essentials for hadoop
 
From DOT to Dotty
From DOT to DottyFrom DOT to Dotty
From DOT to Dotty
 
Oops in java
Oops in javaOops in java
Oops in java
 
06 Java Language And OOP Part VI
06 Java Language And OOP Part VI06 Java Language And OOP Part VI
06 Java Language And OOP Part VI
 
Object-Oriented Programming Concepts
Object-Oriented Programming ConceptsObject-Oriented Programming Concepts
Object-Oriented Programming Concepts
 

Similar to Kotlin's Interoperability with Java

Programming with Kotlin
Programming with KotlinProgramming with Kotlin
Programming with Kotlin
David Gassner
 
Kotlin Online Training.pdf
Kotlin Online Training.pdfKotlin Online Training.pdf
Kotlin Online Training.pdf
SpiritsoftsTraining
 
Kotlin Online Training.pdf
Kotlin Online Training.pdfKotlin Online Training.pdf
Kotlin Online Training.pdf
SpiritsoftsTraining
 
Kotlin Online Training.pdf
Kotlin Online Training.pdfKotlin Online Training.pdf
Kotlin Online Training.pdf
SpiritsoftsTraining
 
Kotlin Online Training.pdf
Kotlin Online Training.pdfKotlin Online Training.pdf
Kotlin Online Training.pdf
SpiritsoftsTraining
 
Kotlin Online Training.pdf
Kotlin Online Training.pdfKotlin Online Training.pdf
Kotlin Online Training.pdf
SpiritsoftsTraining
 
Kotlin Online Training.pdf
Kotlin Online Training.pdfKotlin Online Training.pdf
Kotlin Online Training.pdf
SpiritsoftsTraining
 
Kotlin Online Training.pdf
Kotlin Online Training.pdfKotlin Online Training.pdf
Kotlin Online Training.pdf
SpiritsoftsTraining
 
Kotlin Online Training.pdf
Kotlin Online Training.pdfKotlin Online Training.pdf
Kotlin Online Training.pdf
SpiritsoftsTraining
 
Kotlin Online Training.pdf
Kotlin Online Training.pdfKotlin Online Training.pdf
Kotlin Online Training.pdf
SpiritsoftsTraining
 
Kotlin Online Training.pdf
Kotlin Online Training.pdfKotlin Online Training.pdf
Kotlin Online Training.pdf
SpiritsoftsTraining
 
Kotlin Native - C / Swift Interop - ACCU Autmn 2019
Kotlin Native - C / Swift Interop - ACCU Autmn 2019Kotlin Native - C / Swift Interop - ACCU Autmn 2019
Kotlin Native - C / Swift Interop - ACCU Autmn 2019
Eamonn Boyle
 
Kotlin Online Training.pdf
Kotlin Online Training.pdfKotlin Online Training.pdf
Kotlin Online Training.pdf
SpiritsoftsTraining
 
Kotlin Language powerpoint show file
Kotlin Language powerpoint show fileKotlin Language powerpoint show file
Kotlin Language powerpoint show file
Saurabh Tripathi
 
Kotlin Presentation
Kotlin PresentationKotlin Presentation
Kotlin Presentation
Shayan Pourvatan
 
Intro to Kotlin
Intro to KotlinIntro to Kotlin
Intro to Kotlin
Magda Miu
 
Getting Started With Kotlin
Getting Started With KotlinGetting Started With Kotlin
Getting Started With Kotlin
Gaurav sharma
 
Transitioning Android Teams Into Kotlin
Transitioning Android Teams Into KotlinTransitioning Android Teams Into Kotlin
Transitioning Android Teams Into Kotlin
Garth Gilmour
 
Kotlin for Android - Goto Copenhagan 2019
Kotlin for Android - Goto Copenhagan 2019Kotlin for Android - Goto Copenhagan 2019
Kotlin for Android - Goto Copenhagan 2019
Eamonn Boyle
 
Kotlin from-scratch
Kotlin from-scratchKotlin from-scratch
Kotlin from-scratch
Franco Lombardo
 

Similar to Kotlin's Interoperability with Java (20)

Programming with Kotlin
Programming with KotlinProgramming with Kotlin
Programming with Kotlin
 
Kotlin Online Training.pdf
Kotlin Online Training.pdfKotlin Online Training.pdf
Kotlin Online Training.pdf
 
Kotlin Online Training.pdf
Kotlin Online Training.pdfKotlin Online Training.pdf
Kotlin Online Training.pdf
 
Kotlin Online Training.pdf
Kotlin Online Training.pdfKotlin Online Training.pdf
Kotlin Online Training.pdf
 
Kotlin Online Training.pdf
Kotlin Online Training.pdfKotlin Online Training.pdf
Kotlin Online Training.pdf
 
Kotlin Online Training.pdf
Kotlin Online Training.pdfKotlin Online Training.pdf
Kotlin Online Training.pdf
 
Kotlin Online Training.pdf
Kotlin Online Training.pdfKotlin Online Training.pdf
Kotlin Online Training.pdf
 
Kotlin Online Training.pdf
Kotlin Online Training.pdfKotlin Online Training.pdf
Kotlin Online Training.pdf
 
Kotlin Online Training.pdf
Kotlin Online Training.pdfKotlin Online Training.pdf
Kotlin Online Training.pdf
 
Kotlin Online Training.pdf
Kotlin Online Training.pdfKotlin Online Training.pdf
Kotlin Online Training.pdf
 
Kotlin Online Training.pdf
Kotlin Online Training.pdfKotlin Online Training.pdf
Kotlin Online Training.pdf
 
Kotlin Native - C / Swift Interop - ACCU Autmn 2019
Kotlin Native - C / Swift Interop - ACCU Autmn 2019Kotlin Native - C / Swift Interop - ACCU Autmn 2019
Kotlin Native - C / Swift Interop - ACCU Autmn 2019
 
Kotlin Online Training.pdf
Kotlin Online Training.pdfKotlin Online Training.pdf
Kotlin Online Training.pdf
 
Kotlin Language powerpoint show file
Kotlin Language powerpoint show fileKotlin Language powerpoint show file
Kotlin Language powerpoint show file
 
Kotlin Presentation
Kotlin PresentationKotlin Presentation
Kotlin Presentation
 
Intro to Kotlin
Intro to KotlinIntro to Kotlin
Intro to Kotlin
 
Getting Started With Kotlin
Getting Started With KotlinGetting Started With Kotlin
Getting Started With Kotlin
 
Transitioning Android Teams Into Kotlin
Transitioning Android Teams Into KotlinTransitioning Android Teams Into Kotlin
Transitioning Android Teams Into Kotlin
 
Kotlin for Android - Goto Copenhagan 2019
Kotlin for Android - Goto Copenhagan 2019Kotlin for Android - Goto Copenhagan 2019
Kotlin for Android - Goto Copenhagan 2019
 
Kotlin from-scratch
Kotlin from-scratchKotlin from-scratch
Kotlin from-scratch
 

Recently uploaded

Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
Sven Peters
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
Alina Yurenko
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
Green Software Development
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
Quickdice ERP
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
Rakesh Kumar R
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
Philip Schwarz
 
WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
Patrick Weigel
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
Grant Fritchey
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
mz5nrf0n
 
Lecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptxLecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptx
TaghreedAltamimi
 
zOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL DifferenceszOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL Differences
YousufSait3
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
kalichargn70th171
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
Remote DBA Services
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
dakas1
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
ICS
 
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
gapen1
 
Preparing Non - Technical Founders for Engaging a Tech Agency
Preparing Non - Technical Founders for Engaging  a  Tech AgencyPreparing Non - Technical Founders for Engaging  a  Tech Agency
Preparing Non - Technical Founders for Engaging a Tech Agency
ISH Technologies
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
dakas1
 
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
XfilesPro
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
Green Software Development
 

Recently uploaded (20)

Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
 
WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
 
Lecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptxLecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptx
 
zOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL DifferenceszOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL Differences
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
 
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
 
Preparing Non - Technical Founders for Engaging a Tech Agency
Preparing Non - Technical Founders for Engaging  a  Tech AgencyPreparing Non - Technical Founders for Engaging  a  Tech Agency
Preparing Non - Technical Founders for Engaging a Tech Agency
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
 
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
 

Kotlin's Interoperability with Java

  • 1. NEW BEST FRIENDS: Kotlin’s Interoperability with Java Presented by: David Greenhalgh
  • 2. TODAY’S FORMAT Housekeeping/Intro Kotlin Talk by David Live Q&A Lunch & Learn / Book Giveaway
  • 3. BIG NERD RANCH Travel to your office Discuss Kotlin Enjoy lunch!
  • 4. BUILD BRILLIANCE BIG NERD RANCH • Immersive BOOTCAMPS • Onsite TEAM TRAINING • THE FRONTIER subscription • Digital PRODUCT DEVELOPMENT 4
  • 7. BIG NERD RANCH Travel to your office Discuss Kotlin Enjoy lunch!
  • 9. Why Kotlin? • Concise • Pragmatic • Flexible • Type-safe Why Kotlin? • Concise • Pragmatic Java person.getName(); Kotlin person.name
  • 10. Why Kotlin? • Type-safe Objects may not hold null values unless declared using a nullable type String may not be null String? may be null Why Kotlin? • Full interoperability with Java
  • 11. Interoperability Kotlin code and Java code works together directly • Call Java methods from Kotlin • Call Kotlin functions from Java The Kotlin Compilation Process Kotlin compiles down to Java bytecode
  • 13. Interoperability Kotlin code and Java code works together directly • Leverage legacy Java frameworks (e.g. Android) • Gradually transition your codebase from Java to Kotlin An Example • Calling a Java method from Kotlin • Nullability and platform types • Accessing Java fields from Kotlin • Type mapping • Kotlin file anatomy
  • 14. A Java Method public class Jhava { public String utterGreeting() { return "BLARGH"; } } Calling a Java Method from Kotlin fun main(args: Array<String>) { val adversary = Jhava() println(adversary.utterGreeting()) }
  • 15. Another Java Method public class Jhava { public String utterGreeting() { return "BLARGH"; } public String determineFriendshipLevel() { return null; } } Platform Type
  • 16. NullPointerException in Kotlin fun main(args: Array<String>) { ... val friendshipLevel = adversary.determineFriendshipLevel() println(friendshipLevel.toLowerCase()) } Nullability Annotations public class Jhava { @NotNull public String utterGreeting() { return "BLARGH"; } @Nullable public String determineFriendshipLevel() { return null; } }
  • 17. IntelliJ Nullable Type Awareness Java Field with Getter and Setter public class Jhava { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
  • 18. Property Reference from Kotlin fun main(args: Array<String>) { Jhava().name } Type Mapping Some Kotlin types map directly to Java types • String to String
  • 19. Type Mapping Other Kotlin types do not map directly to Java types • Kotlin's Int to Java's int Primitive Types • Java represents basic data types using "primitve types" (non-objects) • Kotlin represents all data using objects
  • 20. Integers in Kotlin fun main(args: Array<String>) { val adversary = Jhava() val adversaryHitPoints: Int = adversary.hitPoints adversaryHitPoints.dec() println(adversaryHitPoints.javaClass) } >>> int Kotlin File Anatomy • Java files contain exactly one class • Kotlin files may contain classes, functions, and variables at top-level • How can you call standalone Kotlin functions from Java?
  • 21. A Standalone Kotlin Function (Hero.kt) fun main(args: Array<String>) { ... } fun makeProclamation() = "Greetings, beast!" Calling a File-level Function from Java public static void main(String[] args) { System.out.println(HeroKt.makeProclamation()); }
  • 22. Customizing Class Name (Hero.kt) @file:JvmName("Hero") fun main(args: Array<String>) { ... } fun makeProclamation() = "Greetings, beast!" Using the Customized Class Name public static void main(String[] args) { System.out.println(Hero.makeProclamation()); }
  • 23. Customizing Compiled Java Code JVM annotations exist to customize the Java code that is compiled from your Kotlin source • @JvmName • @JvmOverloads • @JvmStatic • @JvmField Refrain: Your Upgrade Path Kotlin compiles down to Java bytecode
  • 24. Risks of Sticking with Java • Slow moving language development • Less type-safe • Less concise • Less interest from developers Next Steps • Start a new project in Kotlin • Upgrade selectively • Acquire Kotlin resources