Java Interop Workbook


Practice problems for Calling Java From Kotlin (and Back) Without Friction. Each takes a minute or two. Write your own answer first, then click Show answer — nothing here is a trick question, just direct practice of the syntax from the lesson.

calling Java from Kotlin

1. A getter is a property

A Java Person has getName() and setAge(int). Read the name and set the age to 30, Kotlin-style.

Show answer Hide answer
val name = person.name
person.age = 30

Kotlin sees a Java getter/setter pair as a property.

2. Pin a platform type

person.getName() returns a platform type. Bind it to a Kotlin val that asserts non-null so a null fails fast here.

Show answer Hide answer
val name: String = person.name

3. SAM conversion

A Java executor.submit(Runnable) expects a Runnable. Submit a task that calls doWork() without writing an anonymous class.

Show answer Hide answer
executor.submit { doWork() }

Kotlin converts the lambda to the single-method Java interface automatically.

calling Kotlin from Java

4. A real static

Expose a companion load() so Java can call Config.load() rather than Config.Companion.load().

Show answer Hide answer
class Config {
    companion object {
        @JvmStatic fun load(): Config = Config()
    }
}

5. Overloads for Java callers

Java can’t call a function and skip parameters. Make connect(host, port = 443) callable from Java both with and without the port.

Show answer Hide answer
@JvmOverloads
fun connect(host: String, port: Int = 443) { /* ... */ }

@JvmOverloads generates the overloads default arguments made unnecessary in Kotlin.

6. Declare a checked exception

A read() function can throw IOException, and you want Java’s compiler to see it. Add the annotation.

Show answer Hide answer
@Throws(IOException::class)
fun read(): String { /* ... */ }

Needed precisely because Kotlin has no checked exceptions of its own.

7. Name the file class

Top-level functions in Strings.kt land in a StringsKt class for Java. Make Java call them on StringUtils instead.

Show answer Hide answer
@file:JvmName("StringUtils")
package text

This annotation goes at the very top of the file, before package.

gotchas

8. Unit becomes void

Write a Kotlin log(msg: String) that returns no meaningful value, and note (as a comment) the signature Java sees.

Show answer Hide answer
fun log(msg: String) {
    println(msg)
}
// Java sees: void log(String msg)

A Kotlin Unit return compiles to void; conversely a Java void method is Unit in Kotlin.

9. Protect a list crossing into Java

A Kotlin read-only List is still mutable from Java (it’s just java.util.List at runtime). Write a function that hands Java a copy nothing can mutate behind your back.

Show answer Hide answer
fun snapshot(items: List<String>): List<String> = items.toList()

toList() returns a fresh list, so a Java caller’s .add() can’t disturb the original.

10. A plain field

Expose a Kotlin property to Java as a public field with no getter/setter pair.

Show answer Hide answer
class Point {
    @JvmField val x: Int = 0
}

Back to the lesson, Calling Java From Kotlin (and Back), or on to the next one: the scope functions.