Member-only story
Under the Hood: How Kotlin Compiles Default Parameters (Bitmasks, $default, and JVM Secrets)
Not a Medium Member? “Read For Free”
If you’ve ever transitioned from Java to Kotlin, one of the first features that probably made you weep tears of joy was default parameters. No more writing five different overloaded constructors or methods just to make a few arguments optional.
The Problem in Java (The Boilerplate Nightmare)
To achieve flexibility in Java, you had to write a cascade of telescoping overloads:
// Java-style pain
public void greet(String name) { greet(name, "Hello"); }
public void greet(String name, String message) { greet(name, message, "!"); }
public void greet(String name, String message, String punctuation) { ... }The Kotlin Solution
In Kotlin, you condense all of that redundant code into a single, clean declaration:
fun greet(name: String, message: String = "Hello", punctuation: String = "!") {
println("$message, $name$punctuation")
}It feels like magic. But Java doesn’t support default parameters natively. So, how does the JVM — which only understands standard bytecode — execute this?










