Table of contents
1.
Introduction
2.
Calling Java from Kotlin 
2.1.
Calling a void method of Java in Kotlin File
2.2.
Calling int and the other methods of Java in Kotlin File
2.3.
Calling Java class present inside the package in the Kotlin code
2.4.
Accessing Java Arrays in Kotlin Array
2.5.
Accessing Java Varargs in Kotlin Code
2.6.
Mapped Types in Kotlin and Java
2.6.1.
Java Primitive Types to the corresponding Kotlin Types
2.6.2.
Java Non-primitive Types to the corresponding Kotlin Types
3.
FAQs
4.
Key Takeaways
Last Updated: Mar 27, 2024

Kotlin Java Compatibility

Author Vivek Goswami
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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:

  1. There must be only one varargs parameter in the function.
  2. 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. 

FAQs

  1. What is returned when the void method of Java is called in the Kotlin file?
    Kotlin returns Unit when we call a void method of Java code inside the Kotlin File.
     
  2. How is Kotlin interoperable with Java?
    Since both languages produce bytecodes, it is easy to Java code in the Kotlin file.
     
  3. Can we use primitive such as float, int, and double in Kotlin?
    No, we can't use them on a language level but certainly by JVM bytecode.

Key Takeaways

Cheers if you reached here!! 

The purpose of this article was to introduce you to Kotlin's Compatibility/Interoperability with Java; we learned how to use different methods of Java code in the Kotlin file and how various types are mapped into Kotlin and Java.

If you want to learn more about decision statements in Kotlin, you can jump to this article which covers them in great detail.

However, learning never stops, and there is more to learn. So head over to our Android Development Course on the Coding Ninjas Website to dive deep into Android Development and build future applications. Till then, Happy Learning!

Live masterclass