In these webinar viewers learned:
How does Kotlin remedy common issues within your codebase?
How can you upgrade to Kotlin without disrupting your previous work?
What is the risk of sticking with Java?
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