Skip to workspace
Kotlin Lab

Kotlin on the JVM / Chapter 02

Keep Java call sites compiling

Expose Kotlin defaults through the JVM methods that Java clients actually call.

The decision

A Java ticket client already calls label(12) and label(12, "OPS"). Migrate its formatter to Kotlin while keeping both public instance methods. The short overload uses the team ENG.

Read the Kotlin

Count the calls each language can express

Kotlin understands default parameters when compiling Kotlin source. A Java member call needs a JVM method with a matching signature. For the label method, @JvmOverloads generates a one-argument overload for the trailing default along with the full method. Removing the annotation leaves the simple Kotlin call valid while a Java caller loses that overload.

class Printer {
    @JvmOverloads fun print(id: Int, copies: Int = 1) = "$id × $copies"
}
// Java can call new Printer().print(7) or .print(7, 2).

Preserve each published Java signature

List the old entry points including their owner, parameter types and whether each is an instance method. An explicit short member can supply the default and delegate to the full member. This also gives a precise migration when only particular overloads are part of the public API. Compile a Java call to each signature to verify the bridge.

fun label(id: Int) = label(id, "OPS")
fun label(id: Int, team: String) = "$team-$id"

Distinguish an omitted argument from a supplied value

The default expression runs when the argument is omitted. An empty string still fills a String parameter, just as zero still fills an Int parameter. The full overload must forward that explicit value, even if it looks like a placeholder. Compare a Java call that omits the trailing argument with one that supplies its empty or zero value.

The full example

Java 25
public class Lesson {
    public static final class TicketLabels {
        public String label(int number) {
            return label(number, "ENG");
        }

        public String label(int number, String team) {
            return team + "-" + number;
        }
    }
}
Kotlin/JVM
class TicketLabels {
    @JvmOverloads
    fun label(number: Int, team: String = "ENG"): String =
        "$team-$number"
}

Inputs and outcomes

InputOutcomeWhy it matters
label(12)ENG-12The Java caller supplies only a number.
label(12, "OPS")OPS-12An explicit team takes precedence.
label(0, "")-0An explicit empty team is a real argument.

Walk through the code

  1. Java delegates the one-argument method to the two-argument implementation. That makes both JVM method signatures visible to a compiled Java caller.
  2. A Kotlin default lets Kotlin callers omit team. Adding @JvmOverloads also emits the one-argument public JVM method. In this signature it forwards to the default team while the two-argument method keeps the supplied team.
  3. The exercise looks up label(int) as a Java public method and invokes it. A Kotlin-only call to label(12) would succeed with a default even when a Java consumer could no longer link.

The Java habit

A successful Kotlin call with an omitted argument proves the Kotlin default works. Java source overload resolution still needs a matching public method, and an existing binary links by JVM descriptor.

When to choose differently

For a configuration with many independent optional settings, generating overloads offers only a succession of shorter signatures. A Java builder can name individual choices without forcing a particular argument order.

References

Open quizTry code