VOOZH about

URL: https://www.geeksforgeeks.org/kotlin/java-interoperability-calling-java-from-kotlin/

⇱ Java Interoperability - Calling Java from Kotlin - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Interoperability - Calling Java from Kotlin

Last Updated : 8 Jun, 2025

Since Kotlin was designed with full interoperability in mind, it allows us to easily use Java classes, methods, and fields from within Kotlin. This seamless integration makes it simple to reuse existing Java codebases while writing modern Kotlin code. In this article, we will understand how Kotlin interacts with Java.

Accessing Getters and Setters

The getters and setters of all the types defined within the Java class are represented as properties in Kotlin. Hence, to access the getters and setters of a data member of a Java class, it must reference as a property within Kotlin.

Example - myjava.java:  


Example - mykotlin.kt:


Output:

5

Kotlin recognizes getValue() and setValue() as property access for value, making the syntax cleaner and more idiomatic.

Calling Java Methods from Kotlin

Calling the Java methods from within Kotlin is a straightforward concept. The types of arguments provided are the same in both Java and Kotlin and the same is the case with the return type of the function. The only exception to this rule is the void return type. Those functions in Java that have a void return type, return a Unit type in Kotlin. So this value can be stored in Kotlin as a Unit exists as a type.

Example - myjava.java:


Example - mykotlin.kt:


Output:

The sum of two numbers is 9

Handling Kotlin Keywords as Java Identifiers

Sometimes, Java method names may conflict with Kotlin keywords like object, sealed, or any. Kotlin allows us to call such methods using backticks.

Example - Java:


Example - Kotlin:


Accessing Java Static Members

The static members of a class in Java become the members of a companion object in Kotlin. However, these companion objects cannot be used directly in expressions. To access, its members use the fully qualified name of the member as defined in Java.

Example - myjava.java:


Example - mykotlin.kt:


Output:

Call successful

Java Arrays vs Kotlin Arrays

Kotlin treats arrays differently from Java.

  • Kotlin arrays are invariant, meaning an Array<String> cannot be assigned to an Array<Any>.
  • Java arrays are covariant, which allows assigning a String[] to an Object[].

To ensure performance and compatibility, Kotlin provides specialized classes for primitive arrays like IntArray, ByteArray, etc. These map directly to Java primitive arrays.

Example - myjava.java:


Example - mykotlin.kt:


Output:

The sum of an array is 21

Java varargs

Java supports the concept of variable-length arguments in functions i.e when the number of arguments to a function is not known in advance, but their type is known, we declare a varargs parameter. Kotlin doesn't provide varargs parameters, however, to be fully operational with Java, it supports a special spread operator (*) to call the functions which have varargs parameters.

Example - myjava.java:


Example - mykotlin.kt:


Output:

Geeks 10 20 30 

Java and Kotlin Mapped Types

Types in Kotlin are different from the types in Java. However, to maintain interoperability Kotlin provides a mapping from Java types to Kotlin types. This mapping takes place at compile time and no significant change in performance at runtime is observed.

Java primitive types are mapped to the following primitive types: 

Java TypeKotlin Type
bytekotlin.Byte
shortkotlin.Short
intkotlin.Int
longkotlin.Long
charkotlin.Char
floatkotlin.Float
doublekotlin.Double
booleankotlin.Boolean

Some of the built-in classes are defined in java.lang package is also mapped to Kotlin classes.

Java TypeKotlin Type
java.lang.Objectkotlin.Any!
java.lang.Cloneablekotlin.Cloneable!
java.lang.Comparablekotlin.Comparable!
java.lang.Enumkotlin.Enum!
java.lang.annotationkotlin.Annotation!
java.lang.CharSequencekotlin.CharSequence
java.lang.Stringkotlin.String!
java.lang.Numberkotlin.Number!
java.lang.Throwablekotlin.Throwable!

The boxed types of Java's primitive data types are mapped to nullable types in Kotlin.

Java TypeKotlin Type
java.lang.Bytekotlin.Byte?
java.lang.Shortkotlin.Short?
java.lang.Integerkotlin.Int?
java.lang.Longkotlin.Long?
java.lang.Characterkotlin.Char?
java.lang.Floatkotlin.Float?
java.lang.Doublekotlin.Double?
java.lang.Booleankotlin.Boolean?
Comment
Article Tags:
Article Tags:

Explore