Java Interop: Mix Kotlin into Existing Projects

Harry · 23 Sep 2026 · 2 views
Log in to track your progress and mark lessons complete.

Calling Java from Kotlin

Call Java directly; getters become properties: user.name calls getName(). Java lists map to Kotlin (Mutable)List.

Calling Kotlin from Java

  • @JvmStatic - expose companion methods as real Java statics.
  • @JvmOverloads - generate Java overloads for default args.
  • @JvmField - expose a property as a public field.
  • Top-level functions land in a FileNameKt class; rename with @file:JvmName.

Nullability Annotations That Matter

// Java side - helps Kotlin trust your APIs:
public @NotNull String slug(@NotNull String title)

Annotate Java boundaries and Kotlin treats them as non-null - fewer surprises, fewer !!.

Checked Exceptions and SAM

  • Kotlin has no checked exceptions - Java ones become runtime callables.
  • Java functional interfaces convert with lambdas: executor.submit { work() }.
  • Use fun interface in Kotlin for the same SAM trick.

Migration Strategy That Works

  1. New files in Kotlin, old files untouched.
  2. Convert high-value POJOs to data classes first.
  3. Convert utils to extension functions.
  4. Convert one module per sprint with tests green throughout.

Key Points

  • Interop is bidirectional and reliable.
  • Annotations at boundaries buy null safety.
  • Migrate module by module, never big-bang.
Share this post:

Comments (0)

Please login or register to comment.

Create a free account to keep reading

You've enjoyed a free tutorial! Register (it's free) to unlock every tutorial, track your progress and save code.

or sign in with your account

Already have an account? Log in