Skip to main content
Browse topics

Calling Java from Kotlin

3 min read Updated June 30, 2026
Share

Introduction

Calling Java from Kotlin is a fundamental concept every Kotlin developer should understand. Kotlin and Java compile to the same JVM bytecode, so you can mix both languages in one project with minimal friction.

Kotlin on JVM can use existing Java classes directly. In this tutorial you will learn the syntax, walk through a complete example program, study the sample output, and review best practices so you can apply the concept confidently in your own projects.

Definition

  • Kotlin on JVM can use existing Java classes directly.
  • Java getters/setters are accessed as properties in Kotlin.
  • Nullability annotations improve safety when calling Java code.

Syntax

kotlin
val obj = JavaClass()

Calling Java from Kotlin Example Program in Kotlin

kotlin
// JavaClass.java: public class JavaClass { public String greet() { return "Hello"; } }
// Kotlin usage shown conceptually in sample below
class JavaStyleService {
    fun greet() = "Hello from Java style service"
}

fun main(args: Array<String>) {
    val service = JavaStyleService()
    println(service.greet())
}

Sample Output

plaintext
Hello from Java style service

When to use

Use interop when migrating a Java codebase incrementally or calling mature Java libraries from Kotlin.

How it works

  1. The program starts with a main function — the entry point that runs when you execute the file.

  2. Comment: JavaClass.java: public class JavaClass { public String greet() { return "Hello"; } } — documents intent for readers.

  3. Comment: Kotlin usage shown conceptually in sample below — documents intent for readers.

  4. fun greet() = "Hello from Java style service" assigns or updates a value used later in the program.

  5. val service = JavaStyleService() assigns or updates a value used later in the program.

  6. The println(service.greet()) statement writes a line to the console — this produces part of the sample output below.

  7. Kotlin on JVM can use existing Java classes directly.

  8. Run the program in IntelliJ IDEA, Android Studio, or with the Kotlin command-line compiler (kotlinc / kotlin). Compare your console output with the sample output shown below.

Best Practices

Common Mistakes

Key Points

  • Kotlin on JVM can use existing Java classes directly.
  • Java getters/setters are accessed as properties in Kotlin.
  • Nullability annotations improve safety when calling Java code.
  • Test the example locally and verify the output matches the sample.
  • Experiment by changing input values to see how behaviour changes.

Notes

  • Nullability annotations help Kotlin and Java agree on which values can be null at boundaries.
  • Semicolons at the end of statements are optional in Kotlin.

Frequently Asked Questions

What is Calling Java from Kotlin in Kotlin?
Kotlin on JVM can use existing Java classes directly.
When should I use Calling Java from Kotlin?
Use interop when migrating a Java codebase incrementally or calling mature Java libraries from Kotlin.
How is Calling Java from Kotlin different from Java?
Nullability annotations improve safety when calling Java code.
How do I practice this topic?
Copy the example program into IntelliJ IDEA or Android Studio, run it, then modify values or add print statements to confirm your understanding.

Related tutorials

Search tutorials