Lesson 6 of 9 · 24m
Java Interoperability Without Platform-Type Leaks
Control nullability, null-hostile collections, SAM conversions, exceptions, and overload friction where Kotlin and Java meet.
Interop is where Kotlin server migrations either stay civilized or turn into archaeology. Kotlin can call Java easily enough that teams forget there is a boundary at all. There is. It is where nullability information degrades, mutable collection expectations leak, and framework annotations stop meaning what either language expected.
Platform types are decision points
When Kotlin sees a Java type without nullability annotations, it exposes a platform type such as String!. That is not a nullable value and not a non-null value; it is the compiler saying, "you decide."
val raw = javaUserRepository.findName(id) // String!
val safe: String? = raw // honest if Java can return null
val trusted: String = raw // a runtime promiseDecide at the adapter and do not propagate the ambiguity. If the Java code accepts annotations, add them:
@Nullable String findName(@NotNull UUID id);Collections cross with different mutation expectations
Java's mutable and immutable collection distinction is weaker than Kotlin's. A Kotlin List<User> passed into Java can be cast and mutated if the underlying implementation permits it. Defensive copies are appropriate at trust boundaries:
Do not pay this cost everywhere. Pay it where ownership is unclear or a library retains references beyond the call.
Checked exceptions disappear, failure does not
Kotlin has no checked exceptions. Java APIs that use them still throw them. More importantly, a Kotlin caller can throw checked-looking exceptions without declaring them, and Java callers may not handle them. Prefer explicit results inside application code:
sealed interface ChargeResult {
data class Approved(val receiptId: String) : ChargeResult
data class Declined(val reason: String) : ChargeResult
data object Unavailable : ChargeResult
}Interop adapters can translate exceptions into these results once, instead of letting implementation-specific exceptions cross every layer.
Overloads and coroutines need bridges
Kotlin default parameters are not visible to Java callers in the friendly way Kotlin callers expect:
class RetryPolicy @JvmOverloads constructor(
val attempts: Int = 3,
val backoffMillis: Long = 100,
)Java cannot naturally call a suspend function either. Expose a CompletableFuture bridge with scope.future { ... }, and bridge CompletionStage back with await() from kotlinx-coroutines-jdk8. Never wrap blocking Java calls in runBlocking just because the compiler made it easy.
Field notes
- Nullability annotations only help if the Java implementation actually honors them; validate critical adapters with tests.
- Java streams are lazy and often terminal-once. Do not assume they behave like Kotlin sequences.
- AutoCloseable interop is usually clean with
use, but coroutine cancellation still requires cooperative blocking code. - Be careful with Lombok-generated methods: Kotlin sees the bytecode shape, not your source-level intent.
- Interop boundaries are architecture. Put them in adapter packages and review them harder than ordinary Kotlin files.
Was this lesson helpful?