Javatpoint Logo
Javatpoint Logo

Java Interoperability

Kotlin code is fully compatible with Java code. The existing Java code can be easily called form Kotlin code and Kotlin code is also called from Java code in normal way.

Calling Java code from Kotlin

Calling Java void method form Kotlin file

While calling a java code from Kotlin whose return types is void, it will return Unit in Kotlin file. If someone wants to return that value, it will assign to Kotlin file by Kotlin compiler and return Unit. For example:

MyKotlinFile.kt

MyJavaClass.java

Output:

printing inside Java class :15
printing sum inside Kotlin file: kotlin.Unit

Calling Java int method from Kotlin file

While calling a java code of int type or other (rather than void) from Kotlin file, it returns the result in same types. For example, calling an area() method of Java class from Kotlin file returns result in int type.

MyKotlinFile.kt

MyJavaClass.java

Output:

printing area from java insideKotlinfile: 12

Kotlin code calling Java class present inside package

If we want to call the Java codes from Kotlin file both present inside the different package, this requires to import the package name with Java class inside Kotlin file.

For example, a Java class MyJavaClass.java is present inside a package myjavapackageand a Kotlin file MyKotlinFile.kt is present inside mykotlinpackage package. In such case calling Java code from Kotlin file needs to import myjavapackage.MyJavaClass inside Kotlin file.

MyKotlinFile.kt

MyJavaClass.java

Output:

printing area from java inside Kotlin file: 12

Kotlin code access Java getter and setter

As Kotlin is completely interoperability with Java, we can access the getter and setter functionality of Java class (or POJO class). For example, create a getter and setter method in Java class MyJava.java with properties firstName and lastName. These properties are accessed from a Kotlin file MyKotlin.kt by creation object of MyJava.java in Kotlin file.

MyJava.java

MyKotlin.kt

Output:

accessing value using property: Arjun
accessing value using property: Kumar
accessing value using method: Arjun
accessing value using method: Kumar

Kotlin code access Java array

We can simply call Java class method which takes array as an argument from Kotlin file. For example, create method sumValue() which takes array element as parameter in Java class MyJava.java calculating addition and returns result. This method is called from Kotlin file MyKotlin.kt by passing array as parameter.

MyJava.java

MyKotlin.kt

Output:

sum of array element is 15

Kotlin code access Java Varargs

In the Java varags functionality, we can pass any number of arguments to a method. Java varargs parameter is defined using ellipsis i.e. three dots (...) after data type.

Following points are to be kept while using the varargs parameter:

  • There is only one varargs parameter in a method.
  • Varargsagrument must be at the last argument.

While accessing the Java varargs from Kotlin we need to use spread operator * to pass the array.

Let's see an example in which a Java method uses an int type varargs which is called from Kotlin file.

MyJava.java

MyKotlin.kt

Output:

0
1
2
3

Let's see another example which takes two parameter in a Java method uses as parameters of String type and int type varargs called from Kotlin file.

MyJava.java

MyKotlin.kt

Output:

string is hello
0
1
2
3

Kotlin and Java Mapped types

Kotlin and Java types are mapped differently, however they are mapped to corresponding types. Mapping of these types are matters only at compile time and run time remains unchanged.

Java's primitive types to corresponding Kotlin types

Java type Kotlin type
byte kotlin.Byte
short kotlin.Short
int kotlin.Int
long kotlin.Long
char kotlin.Char
double kotlin.Double
boolean kotlin.Boolean

Java's non-primitive types to corresponding Kotlin types

Java type Kotlin type
java.lang.Object kotlin.Any!
java.lang.Cloneable kotlin.Cloneable!
java.lang.Comparable kotlin.Comparable!
java.lang.Enum kotlin.Enum!
java.lang.Annotation kotlin.Annotation!
java.lang.Deprecated kotlin.Deprecated!
java.lang.CharSequence kotlin.CharSequence!
java.lang.String kotlin.String!
java.lang.Number kotlin.Number!
java.lang.Throwable kotlin.Throwable!

Java's boxed primitive types to corresponding nullableKotlin types

Java type Kotlin type
java.lang.Byte kotlin.Byte?
java.lang.Short kotlin.Short?
java.lang.Integer kotlin.Int?
java.lang.Long kotlin.Long?
java.lang.Character kotlin.Char?
java.lang.Float kotlin.Float?
java.lang.Double kotlin.Double?
java.lang.Boolean kotlin.Boolean?

Java's collection types to corresponding read-only or mutable Kotlin types

Java type Kotlin read-only type Kotlin mutable type
Iterator<T> Iterator<T> MutableIterator<T>
Iterable<T> Iterable<T> MutableIterable<T>
Collection<T> Collection<T> MutableCollection<T>
Set<T> MutableSet<T> MutableSet<T>
List<T> MutableList<T> MutableList<T>
ListIterator<T> ListIterator<T> MutableListIterator<T>
Map<K, V> Map<K, V> MutableMap<K, V>
Map.Entry<K, V> Map.Entry<K, V> MutableMap.MutableEntry<K, V>





Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


B.Tech / MCA