Calling Java Codes from Kotlin
By: Kayvan Kaseb
Tags: Android, Java, Android App Development, Interoperability, Kotlin

Calling Java Codes from Kotlin

As a matter of fact, Kotlin is an advanced programming language, which handles some modern features like Interoperability. In other words, two languages can communicate and exchange information with each other. As a result, calling Java codes from Kotlin would be a key factor in designing and implementing codes in Android development. This essay aims to consider some methods for calling Java codes from Kotlin in order to enhance reuseability and productivity of codes in Android.

What is the definition of Interoperability?

Interoperability means the ability of computer system or software to exchange and make use of information. The same concept is used for programming languages. It means the ability of two languages for communicating and exchanging information with each other such as functions and classes. Interoperability is a benefit because various programming languages are optimized for specific tasks, and allowing them to communicate for creating better software. As a matter of fact, it gives you code re-usability, which is a key factor for enhancing software productivity, and also reducing application development and maintenance costs.

Interoperability between Java and Kotlin

In fact, Kotlin code is fully compatible with Java code. Therefore, you can be able to use both the Java and Kotlin languages in a single project due to Interoperability. You can call Kotlin functions in Java as well as Java methods and variables in Kotlin code. For instance, if you have a Java project with classes and functions, then you do not need to rewrite the project in Kotlin from scratch. Alternatively, you can be able to use every line of Java code in Kotlin, and begin writing new functionalities here. Besides, you can call Kotlin codes in Java in the same way. There are some approaches to perform this issue as follows:

Calling Java methods form Kotlin file Calling the Java codes from Kotlin in different packages Accessing to Java array in Kotlin codes Accessing to POJO Java class in Kotlin codes Accessing Java Varargs in Kotlin codes

1. Calling Java methods form Kotlin file

Fundamentally, calling the Java methods from the Kotlin codes is a straightforward concept. To accomplish this task, it must return the result in same types. The only exception to this rule is the void return type. Those functions in Java, which have a void return type, return a Unit type in Kotlin. Thus, this value can be saved in Kotlin as Unit as a type.

// Kotlin codes in SampleKotlin.kt

fun main(args: Array<String>){ val rectangleArea: Int = SampleJavaClass.rectangleArea(10, 20)

println("inside the Kotlin codes:" + rectangleArea)



} //Java codes in SampleJavaClass.java

public class SampleJavaClass{



public static void main(String[] args){



} public static int rectangleArea(int x, int y){ int result = x * y;

return result;

}

}

If you want to call a java code from Kotlin, which return type is void, it will return Unit in Kotlin file. For instance, the output for these codes would be:

adding inside the Java : 170

result inside the Kotlin: kotlin.Unit

//Kotlin codes in SampleKotlin.kt

fun main(args: Array<String>){ val result = SampleJavaClass(110, 60)

println ("sum inside the Kotlin:" + result) } //Java codes in SampleJavaClass.java

public class SampleJavaClass{ public static void main(String[] args){



} public static void add(int a, int b){ int adding = a + b;

System.out.println("adding inside the Java:" + adding); }

}

2. Calling the Java codes from Kotlin in different packages

To call the Java codes from Kotlin file, which both of them are inside different packages, you should import the package name along with the Java class inside Kotlin file. For example:

//Kotlin codes in SampleKotlin.kt

package mykotlinpackage

import mypackage.SampleJava //importing Java class here!

fun main(args: Array<String>){



val result= SampleJava.display()

println(result) } //Java codes in SampleJava.java

package mypackage;

public class SampleJava{

public static void main(String[] args){



}

public static void display(){ System.out.println("call the codes"); }

}

3. Accessing to Java array in Kotlin codes

Basically, we can simply call Java class methods, which take array as an argument from Kotlin file. For example:

//Kotlin codes in SampleKotlin.kt

fun main(args: Array<String>){



val sampleObject = SampleClass()

val array = intArrayOf(2, 4, 6, 8, 10)

var sum = sampleObject.calculate(array)

println("The sum of the array is " + sum) //Output: 30 } //Java Codes in SampleJava.java

public class SampleClass{ int result = 0;

public int calculate(int[] array){ for(int a: array){ result = result + a;

} return result;

}

}

4. Accessing to POJO Java class in Kotlin codes

In general, the getters and setters of all the types, which are defined within the Java class are represented as properties in the Kotlin. Therefore, to access the getters and setters of a data member of a Java class, you must reference as a property within Kotlin. For instance:

//Kotlin codes in SampleKotlin.kt

fun main(args: Array<String>){ val sampleObject = SampleJava()

sampleObject.setValue = 10 //Calling the setter function!

println(sampleObject.value) //Calling the getter function! } // Java class in SampleJava.java

public class SampleJava{ private int value; public int getValue(){ return value;

} public void setValue(int value){ this.value = value;

} }

5. Accessing Java Varargs in Kotlin codes

Principally, we can be able to pass any number of arguments to a method in the via Java Vargas functionality. Java Vargas parameter is defined using ellipsis i.e. three dots (…) after data type. For accessing the Java Vargas from Kotlin codes, you must use spread operator * to pass the array. For example:

//Kotlin codes in SampleKotlin.kt

fun main(args: Array<String>){ val sampleJava = SampleJava()

val array = intArrayOf(2, 4, 6, 8)

sampleJava.display(*array) } //Java codes in SampleJava.java

public class SampleJava{



public void display(int... values){



for (int s : values){ System.out.println(s); }

}

}

Mapped types

Basically, types in Kotlin are completely different from the types in Java, but to maintain interoperability Kotlin indicates mapping from Java types to Kotlin types. This mapping takes place at compile time, and there is no important change in performance at run-time. For instance:

java.lang.Object — — — -> kotlin.Any!

java.lang.Long — — — -> kotlin.Long?

long — — — -> kotlin.Long

In conclusion, Interoperability or calling codes from another language means two languages can communicate and exchange information with each other. So, calling Java codes from Kotlin would be a key factor in designing and implementing codes in Android development. This essay discussed five methods for implementing this issue.