Sitemap

What does the JVM overload Annotation do?

3 min readAug 31, 2025

Kotlin simply explained

Press enter or click to view image in full size
Photo by Louis Tsai on Unsplash

Kotlin is designed for smooth interoperability with Java, but some Kotlin features, such as default parameter values, aren’t natively supported in Java. The @JvmOverloads annotation is a tool that helps bridge this gap, making Kotlin functions with default parameters easier to use from Java code.

What is @JvmOverloads?

The @JvmOverloads annotation tells the Kotlin compiler to automatically generate overloaded versions of a function or constructor for each possible combination of parameters with default values, starting from the last parameter and moving left. This allows Java callers to use these overloads as if they were hand-written, since Java does not support default parameter values.

How Does It Work?

Suppose you have a Kotlin function with default values:

Press enter or click to view image in full size

Kotlin code can call greet(), greet("Alice"), or greet("Alice", "Hi") directly, thanks to default parameters. However, Java does not support default arguments, so without @JvmOverloadsJava could only call the version with all parameters.

Without JVM overload. If we try to call the function in Java.

Let’s try using JVM overload in Kotlin.

Press enter or click to view image in full size

This ensures Java code can call the function with any number of arguments, matching the Kotlin experience.

Where Can You Use @JvmOverloads?

  • On functions (member or top-level)
  • On constructors
  • On methods in object or companion object declarations

Why Do We Use @JvmOverloads?

  • Java Interoperability: Java doesn’t support default parameter values. @JvmOverloads makes Kotlin functions with defaults accessible from Java without requiring all arguments.
  • Cleaner Java API: Reduces boilerplate and manual overload creation, making APIs easier to use and maintain.
  • Consistency: Ensures both Kotlin and Java callers have a similar experience when calling functions with default parameters.

Key Points to Remember

  • Overloads are only generated for parameters with default values, and only from the last parameter moving left.
  • Not all possible combinations are generated (e.g., you can’t skip a middle parameter from Java unless it’s the last one).
  • Use @JvmOverloads when you expect your Kotlin code to be called from Java, especially for libraries or APIs.

Conclusion

The @JvmOverloads annotation is essential for Kotlin-Java interoperability when dealing with default parameter values. It generates the necessary overloads so Java can call Kotlin functions flexibly, just like Kotlin itself. This keeps your code concise, clean, and easy to use across both languages

If you want to learn more. Here is a video for you.

--

--

Hitesh Kohli
Hitesh Kohli

Written by Hitesh Kohli

Hi, my name is Hitesh Kohli, I work at Geeks for Geeks as an Android developer. I love messing around with apps and games.

No responses yet