Industrial Training




Calling Kotlin code from Java


As Kotlin is completely compatible with Java language. It means the application written in Java code can be easily called from Kotlin. In the similar way ,Kotlin code is also called from Java code.
Before discussing how to call Kotlin code from Java code, let's see how Kotlin file internally looks like.


How a simple Kotlin program internally looks like.


Let's create a simple main function in a MyKotlin.kt file.


fun main(args: Array){  
//code  
}  
fun area(l: Int,b: Int):Int{  
    return l*b  
}  

After compiling the above Kotlin file MyKotlin.kt which internally looks like:


public class MyKotlinKt{  
public static void main(String[] args){  
                     //code  
            }  
            public static int area(int l, int b){  
                     return l*b;  
            }  
}  

The Kotlin compiler internally adds a wrapper class with naming convention MyKotlinKt. The Kotlin file MyKotlin.kt is converted into MyKotlinKt and it is public in default. The default modifier of high level function is public and function is converted into static as default. As the return type is Unit in MyKotlin.kt, it is converted into void in MyKotlinKt.


Calling Kotlin code from Java code


MyKotlin.kt
fun main(args: Array){  
//code  
}  
fun area(l: Int,b: Int):Int{  
    return l*b  
}  

MyJava.java
public class MyJava {  
    public static void main(String[] args) {  
int area = MyKotlinKt.area(4,5);  
System.out.print("printing area inside Java class returning from Kotlin file: "+area);  
    }  
}  

Output:
printing area inside Java class returning from Kotlin file: 20

Java code calling Kotlin file present inside package


If we want to call the Kotlin code from Java class both present inside the different packages, this requires to import the package name with Kotlin file name inside Java class and calling the Kotlin code from Java class. Another way is to give full path as packageName.KotlinFileKt.methodName().


MyKotlin.kt
package mykotlinpackage  
  
fun main(args: Array< String>) {  
  
}  
fun area(l: Int,b: Int):Int{  
       return l*b  
}  

MyJava.java
package myjavapackage;  
import mykotlinpackage.MyKotlinFileKt;  
  
public class MyJavaClass {  
    public static void main(String[] args){  
int area = MyKotlinKt.area(4,5);  
System.out.println("printing area inside Java class returning from Kotlin file: "+area);  
    }  
}  

Output:
printing area inside Java class returning from Kotlin file: 20

Changing the Kotlin file name using annotation @JvmName


A Kotlin file name can be changed as wrapper class name using @JvmName annotation.


MyKotlin.kt

Write a Kotlin code and place annotation @file: JvmName("MyKotlinFileName") at the top. After compiling Kotlin code, the file name is changed into the name provided inside annotation (in my caseMyKotlinFileName). While accessing the code of MyKotlin.kt we require to use the file name as MyKotlinFileName.


@file: JvmName("MyKotlinFileName")  
package mykotlinpackage  
  
fun main(args: Array< String>) {  
  
}  
fun area(l: Int,b: Int):Int{  
       return l*b  
}  

MyJava.java
package myjavapackage;  
import mykotlinpackage.MyKotlinFileName;  
  
public class MyJavaClass {  
    public static void main(String[] args){  
int area = MyKotlinFileName.area(4,5);  
System.out.println("printing area inside Java class returning from Kotlin file: "+area);  
    }  
}  

Output:
printing area inside Java class returning from Kotlin file: 20

Calling method of multiple file having same generated Java class name using@JvmMultifileClass


If the Kotlin's multiple files having same generated Java file name using @JvmName annotation, normally give error while calling from Java file. However, Kotlin compiler generates single Java façade class which contains generated Java file and all the declarations of the files which have same names. To active this generation façade, we use @JvmMultifileClass annotation in all the files.


MyKotlin1.kt
@file: JvmName("MyKotlinFileName")  
@file:JvmMultifileClass  
package mykotlinpackage  
  
fun main(args: Array< String>) {  
  
}  
fun area(l: Int,b: Int):Int{  
    return l*b  
}  

MyKotlin2.kt
@file: JvmName("MyKotlinFileName")  
@file:JvmMultifileClass  
package mykotlinpackage  
  
  
fun volume(l: Int,b: Int,h: Int):Int{  
    return l*b*h  
}  

MyJava.java
package myjavapackage;  
import mykotlinpackage.MyKotlinFileName;  
  
public class MyJavaClass {  
    public static void main(String[] args){  
int area = MyKotlinFileName.area(4,5);  
System.out.println("printing area inside Java class returning from Kotlin file: "+area);  
int vol = MyKotlinFileName.volume(4,5,6);  
System.out.println("printing volume inside Java class returning from Kotlin file: "+vol);  
    }  
}  

Output:
printing area inside Java class returning from Kotlin file: 20
printing volume inside Java class returning from Kotlin file: 120

Kotlin property access throughconst modifier


The Kotlin properties which are annotated with const modifier in the top level as well as in class are converted into static fields in Java. These properties are access from Java file as static properties called. For example:


MyKotlin.kt
constval MAX = 239  
object Obj {  
constval CONST = 1  
}  
class C {  
    companion object {  
constval VERSION = 9  
    }  
}  

MyJava.java
public class MyJava {  
    public static void main(String[] args) {  
int c = Obj.CONST;  
int m = MyKotlinKt.MAX;  
int v = C.VERSION;  
System.out.println("const "+c+"\nmax "+m+"\nversion "+v);  
    }  
}  

Output:
const 1
max 239
version 9



Hi I am Pluto.