Industrial Training




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
fun main(args: Array) {  
val sum= MyJavaClass.add(5, 10)  
println("printing sum inside Kotlin file: "+sum)  
 }  

MyJavaClass.java
public class MyJavaClass {  
    public static void main(String[] args){  
  
    }  
    public static void add(inta,int b){  
int result = a + b;  
System.out.println("printing inside Java class :"+result);  
    }  
}  

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
fun main(args: Array) {  
val area: Int = MyJavaClass.area(3, 4)  
println("printing area from java insideKotlin file: "+area)  
}  

MyJavaClass.java
public class MyJavaClass {  
    public static void main(String[] args){  
  
    }  
    public static int area(int l, int b){  
int result = l * b;  
        return result;  
    }  
}  

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
package mykotlinpackage  
import myjavapackage.MyJavaClass  
  
fun main(args: Array) {  
val area: Int = MyJavaClass.area(3, 4)  
println("printing area from java inside Kotlin file: "+area)  
}  

MyJavaClass.java
package myjavapackage;  
  
public class MyJavaClass {  
    public static void main(String[] args){  
  
    }  
    public static int area(int l, int b){  
int result = l * b;  
        return result;  
    }  
}  

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
public class MyJava{  
    protected String firstName;  
    protected String lastName;  
  
    public String getfirstName() {  
        return firstName;  
    }  
    public void setfirstName(String firstName) {  
this.firstName = firstName;  
    }  
    public String getlastName() {  
        return lastName;  
    }  
    public void setlastName(String lastName) {  
this.lastName = lastName;  
    }  
}  

MyKotlin.kt
fun main(args: Array) {  
val myJava = MyJava()  
  
myJava.lastName = "Kumar"  
myJava.setfirstName("Arjun")  
  
println("accessing value using property: "+myJava.firstName)  
println("accessing value using property: "+myJava.lastName)  
  
println("accessing value using method: "+myJava.getfirstName())  
println("accessing value using method: "+myJava.getlastName())  
}  

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
public class MyJava {  
  
    public intsumValues(int[] nums) {  
int result = 0;  
        for (int x:nums) {  
            result+=x;  
        }  
        return result;  
    }  
}  

MyKotlin.kt
fun main(args: Array){  
val myJava = MyJava()  
val numArray = intArrayOf(1, 2, 3,4,5)  
val sum = myJava.sumValues(numArray)  
println(sum)  
}  

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 metho
  • 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
public class MyJava {  
    public void display(int... values) {  
        for (int s : values) {  
System.out.println(s);  
        }  
    }  
}  

MyKotlin.kt
fun main(args: Array){  
val myJava = MyJava()  
val array = intArrayOf(0, 1, 2, 3)  
myJava.display(*array)  
}  

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
public class MyJava {  
    public void display(String message,int... values) {  
System.out.println("string is " + message);  
        for (int s : values) {  
System.out.println(s);  
        }  
    }  
}  

MyKotlin.kt
fun main(args: Array){  
val myJava = MyJava()  
val array = intArrayOf(0, 1, 2, 3)  
myJava.display("hello",*array)  
}  

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
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>



Hi I am Pluto.