Introduction
While designing Kotlin, developers had interoperability with Java in their minds. So we can call existing Java codes in Kotlin in a straightforward way. Similarly, we can also use Kotlin code in Java easily. In this blog, we will mainly focus on using Java code in Kotlin and various methods to do so.
Let's move forward to know how it is done and different methods to help us do so.
Calling Java from Kotlin
Now, we’ll focus on how to call various Java codes and methods from Kotlin and then use it.
Calling a void method of Java in Kotlin File
When we call Java code which has return type as void, Unit will be returned by the Kotlin file.
Kotlin compiler assigns the value to the Kotlin file and returns the Unit.
Let us try to understand it better with an example:
KotlinFile.kt
fun main(args: Array<String>) {
val sum= JavaClass.addition(15, 10)
println("This is the sum inside the Kotlin file: "+sum)
}
JavaClass.java
public class JavaClass{
public static void main(String[] args){
}
public static void addition(int a,int b){
int result ;
result= a+b;
System.out.println("This is printed inside the Java class :"+result);
}
}
Output:
This is printed inside the Java class : 25
This is the sum inside the Kotlin file: kotlin.Unit
Calling int and the other methods of Java in Kotlin File
Whenever we call methods with the return type as anything except void in Java, Kotlin code will return the result in the same type.
Suppose we call int methods of Java return type will also be int in Kotlin.
Let us try to understand how to implement it with the help of an example:
KotlinFile.kt
fun main(args: Array<String>) {
val product: Int = JavaClass.product(5, 8)
println("This is the product from Java code inside Kotlin file: " + product)
}
JavaClass.java
public class JavaClass {
public static void main(String[] args){
}
public static int product(int a, int b){
int answer= a* b;
return answer;
}
}
Output:
This is the product from Java code inside Kotlin file: 40
Calling Java class present inside the package in the Kotlin code
If we call the Java codes in a Kotlin file and both the codes are present inside different packages, then we will import the package's name with the Java class it is present in inside the file of Kotlin.
Suppose a Java class named JavaPackage is present inside the JavaClass class, and we want to call it in a Kotlin file named as KotlinFile.kt, which is present inside the KotlinPacakge. In order to call Java code inside the Kotlin file, we will have to import JavaPackage.JavaClass inside the KotlinFile.kt
KotlinFile.kt
package KotlinPackage
import JavaPackage.JavaClass
fun main(args: Array<String>) {
val product: Int = JavaClass.area(5, 8)
println("This is the product from Java code inside Kotlin file: " + product)
}
JavaClass.java
package JavaPackage;
public class JavaClass {
public static void main(String[] args){
}
public static int product(int a, int b){
int result = a* b;
return result;
}
}
Output:
This is the product from Java code inside Kotlin file: 40
Accessing Java Arrays in Kotlin Array
We can use methods of Java class which take Java arrays as argument simply by calling from Kotlin file bypassing array as an argument.
Suppose we create a method named Product() which returns the product of every element of an array and takes array elements as parameters in the Java class named JavaClass.java and returns void. This method will be called in KotlinFile.kt by passing an array as a parameter.
Let us try to understand it better with an example:
JavaClass.java
public class JavaClass {
public int product(int[] array) {
int result = 1;
for (int x=0;x<array.length;x++) {
result= result*array[x];
}
return result;
}
}
KotlinFile.kt
fun main(args: Array<String>) {
val Javainstance = Java()
val Array = intArrayOf(1, 2, 3, 4, 5)
val ans = Javainstance.Product(Array)
println("Product of array elements is: " + ans)
}
Output:
Product of array elements is: 120
If you want to learn more about the arrays in Kotlin, then read this article.
Accessing Java Varargs in Kotlin Code
In the varargs functionality of Java, we can pass any number of arguments to a method. It is defined by using three dots followed by the data type.
Points of consideration while using varargs are as follows:
- There must be only one varargs parameter in the function.
- It must be the last argument in the method.
When we want to access Java varargs in the Kotlin file, we need to use the spread operator *.
Let’s understand it better with an example:
JavaClass.java
public class JavaClass {
public void showing(int... values) {
for (int ans : values) {
System.out.println(ans);
}
}
}
KotlinFile.kt
fun main(args: Array<String>) {
val Javainstance = JavaClass()
val array = intArrayOf(4, 3, 2, 1)
Javainstance.showing(*array)
}
Output:
4
3
2
1
If you want to learn more about the arrays in Kotlin, then read this article.
Mapped Types in Kotlin and Java
Although Kotlin and Java are mapped differently, they are mapped to corresponding types.
Mapping only matters only at the time of compilation and not on runtime.
Java Primitive Types to the corresponding Kotlin Types
Java Types |
Kotlin Types |
byte |
Kotlin.Byte |
short |
kotlin.Short |
int |
kotlin.Int |
long |
kotlin.Long |
char |
kotlin.Char |
double |
kotlin.Double |
boolean |
kotlin.Boolean |
Java Non-primitive Types to the corresponding Kotlin Types
Java Types |
Kotlin Types |
byte |
kotlin.Byte |
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! |
Above tables name all the primitive and non-primitive types in Java and their corresponding types in Kotlin.




