SlideShare a Scribd company logo
1 of 24
Download to read offline
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 (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 KotlinDavid Gassner
 
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 2019Eamonn Boyle
 
Kotlin Language powerpoint show file
Kotlin Language powerpoint show fileKotlin Language powerpoint show file
Kotlin Language powerpoint show fileSaurabh Tripathi
 
Intro to Kotlin
Intro to KotlinIntro to Kotlin
Intro to KotlinMagda Miu
 
Getting Started With Kotlin
Getting Started With KotlinGetting Started With Kotlin
Getting Started With KotlinGaurav sharma
 
Transitioning Android Teams Into Kotlin
Transitioning Android Teams Into KotlinTransitioning Android Teams Into Kotlin
Transitioning Android Teams Into KotlinGarth Gilmour
 
Kotlin for Android - Goto Copenhagan 2019
Kotlin for Android - Goto Copenhagan 2019Kotlin for Android - Goto Copenhagan 2019
Kotlin for Android - Goto Copenhagan 2019Eamonn Boyle
 

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

Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyAnusha Are
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfayushiqss
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfVishalKumarJha10
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfryanfarris8
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfproinshot.com
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxalwaysnagaraju26
 

Recently uploaded (20)

Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodology
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 

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