Kotlin

Lesson 09

JVM interop platform types

Treat Java platform types as nullable until Kotlin code proves the contract.

Good Code

src/main/kotlin/reviews/JavaReviewAdapter.kt
fun reviewerName(javaReview: JavaReview?): String? {
    // Java values are checked before Kotlin treats the name as non-null.
    val name = javaReview?.reviewerName ?: return null
    return name.trim().takeIf { it.isNotEmpty() }
}

Bad Code

JavaReviewAdapter.kt
fun reviewerName(javaReview: JavaReview): String {
    // Platform types can still carry null from Java callers.
    return javaReview.reviewerName.trim()
}

Review Notes

What to review

Good Code

The good version checks the Java object and the Java-provided name before returning a Kotlin string.

Bad Code

The bad version trusts a platform type. If Java returns null, Kotlin code fails away from the interop boundary.

Takeaways

  • Kotlin review should add a boundary check when Java APIs return values without nullability metadata.