Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Kotlin-Java Interoperability | Kotlin Foundations and Interoperability
Kotlin for Java Developers

bookKotlin-Java Interoperability

Swipe um das Menü anzuzeigen

Kotlin and Java: Working Together on the JVM

Kotlin is fully interoperable with Java because both languages compile to Java bytecode and run on the Java Virtual Machine (JVM). This means you can use Kotlin and Java code side by side in the same project without special configuration. The JVM acts as a common platform, allowing seamless integration between the two languages.

Calling Java from Kotlin

Kotlin can directly use Java classes, methods, and libraries. You can:

  • Import and instantiate Java classes in Kotlin files;
  • Call Java methods and access Java fields as if they were written in Kotlin;
  • Use existing Java libraries, frameworks, and tools without modification;
  • Handle Java exceptions using Kotlin's try-catch syntax.

Java's nullability is mapped to Kotlin's type system, so you can safely interact with Java code while taking advantage of Kotlin's null safety features.

Calling Kotlin from Java

Java code can call Kotlin classes and functions just like any other Java code. When you compile Kotlin code, it generates standard Java bytecode, so:

  • Kotlin classes, methods, and properties are accessible from Java;
  • Kotlin features like default arguments and extension functions are handled with generated methods to maintain compatibility;
  • You can annotate Kotlin code to adjust visibility or behavior for Java callers using annotations like @JvmStatic, @JvmOverloads, and @JvmField.

Compatibility and Migration

Kotlin is designed to be 100% compatible with Java. You can:

  • Gradually migrate Java code to Kotlin, file by file, without breaking your application;
  • Mix Java and Kotlin files in the same module or project;
  • Reuse existing Java libraries and frameworks in Kotlin projects without wrappers or adapters.
question mark

Which statements about Kotlin-Java interoperability are correct

Select all correct answers

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 3

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Abschnitt 1. Kapitel 3
some-alt