← Back to Kotlin Course | Chapter 14: JVM Interop & Best Practices | Lesson 1 of 7

Java Interop

Java interop means Kotlin and Java code can call each other directly, since they both ultimately run on the same JVM.

Calling Java Standard Library Classes

Kotlin can use any Java class directly, including the entire Java standard library, since Kotlin compiles down to the same JVM bytecode Java does.

Example: Calling Java Standard Library Classes

markup
import java.util.ArrayList

fun main() {
    val javaList = ArrayList<String>()
    javaList.add("Kotlin")
    javaList.add("Java")
    println(javaList)
}

Platform Types from Java

A value coming from Java code with no nullability annotation is treated by Kotlin as a 'platform type', shown as String! internally, which skips strict compile-time null checking until you decide how to treat it.

Note: Always add an explicit null check around values coming from Java APIs without nullability annotations.

Example: Platform Types from Java

markup
fun getJavaStyleValue(): String? {
    // Simulating a Java method with unknown nullability
    val value: String? = if (System.currentTimeMillis() % 2 == 0L) "value" else null
    return value
}

fun main() {
    val result = getJavaStyleValue()
    println("Result: ${result ?: "was null"}")
}

Using Java Getters and Setters as Properties

Kotlin lets you call a Java class's getX()/setX() methods using property-style syntax, such as obj.x instead of obj.getX(), when calling from Kotlin.

Example: Using Java Getters and Setters as Properties

markup
import java.util.Calendar

fun main() {
    val calendar = Calendar.getInstance()
    val year = calendar.get(Calendar.YEAR)
    println("Current year (via Java API): $year")
}

Kotlin Functions Called from Java

A top-level Kotlin function compiles into a static method on an automatically generated class named after its file (e.g. functions in Utils.kt become static methods on UtilsKt), which is how Java code calls them.

Example: Kotlin Functions Called from Java

markup
fun greet(name: String): String = "Hello, $name!"

fun main() {
    // Java code would call this as UtilsKt.greet("Kotlin")
    println(greet("Kotlin"))
}
Common Mistakes
  1. Forgetting that a Java method returning a plain (non-annotated) reference type appears to Kotlin as a 'platform type', which skips Kotlin's null-safety checks until you add your own.
  2. Assuming every Java naming convention carries over unchanged; Kotlin has some special call syntax for Java getters/setters (property syntax) that Java methods don't have natively.
  3. Not realizing a Kotlin top-level function compiles to a static method on an automatically generated class (like FileNameKt) when called from Java.
Chapter Summary
  • Kotlin code can call Java classes and methods directly, and Java code can call Kotlin classes and functions directly, since both compile to JVM bytecode.
  • A Java method's return type with no nullability annotation becomes a 'platform type' in Kotlin, which bypasses compile-time null checks.
  • Kotlin can access Java getter/setter pairs (getX()/setX()) using property-style syntax (obj.x).
  • Kotlin top-level functions become static methods on a generated class named after the file when called from Java.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.