8

Calling Kotlin from Java - Interoperability - Kotlin Android

 5 years ago
source link: https://kotlin-android.com/calling-kotlin-from-java/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Kotlin from Java
Kotlin from Java - example

In this tutorial, We are going to call some Koltin functions from Java. Calling Kotlin code from Java is Kotlin-Java interoperability. Java can call almost every type of Kotlin code. Kotlin is designed in that way.

In the other tutorial we learned about how to call Java code from a kotlin file. This time its a reverse case.

Kotlin from Java

To demonstrate this concept. Let me create a Java class and a Kotlin file. I named Java class as CustomJavaClass

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

        System.out.println("I am Java class");
    }
}
  1. public class CustomJavaClass {
  2. public static void main(String[] args) {
  3. System.out.println("I am Java class");
public class CustomJavaClass {
    public static void main(String[] args) {

        System.out.println("I am Java class");
    }
}

Here is the Kotlin file. Kotlin file has a function named sumNumbers. The Kotlin file that has the main and sumNumbers functions is named as mainClass.kt. Our goal is to call this sumNumbersfrom the Java class.

fun main(args: Array<String>) {
    
}

fun sumNumbers(value: Int, value2: Int): Int {
    return value + value2

}
  1. fun main(args: Array<String>) {
  2. fun sumNumbers(value: Int, value2: Int): Int {
  3. return value + value2
fun main(args: Array<String>) {
    
}

fun sumNumbers(value: Int, value2: Int): Int {
    return value + value2

}

Now I am going to call the Kotlin’s sumNumber function from the Java class. This is how it looks like

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

        int sum = MainClassKt.sumNumbers(10, 12);
        System.out.println("The sum returned from Kotlin's sumNumbers function is " + sum);
    }
}
  1. public class CustomJavaClass {
  2. public static void main(String[] args) {
  3. int sum = MainClassKt.sumNumbers(10, 12);
  4. System.out.println("The sum returned from Kotlin's sumNumbers function is " + sum);
public class CustomJavaClass {
    public static void main(String[] args) {

        int sum = MainClassKt.sumNumbers(10, 12);
        System.out.println("The sum returned from Kotlin's sumNumbers function is " + sum);
    }
}

The result is

The sum returned from Kotlin's sumNumbers function is 22
  1. The sum returned from Kotlin's sumNumbers function is 22
The sum returned from Kotlin's sumNumbers function is 22

It worked perfectly fine. They only thing we did is wrote the Koltin filename MainClassKt and then called the function.

Do you remember which functions we used to call with a class name? Yes, exactly the static functions.

This is the same thing is happening here. The compiler is converting the Kotlin class to a static Java class and function is also a static function.

/*public class MainClassKt {
    public static void main(String[] args) {

    }

    public static int sumNumbers(int value, int value2) {
        return value + value2;

    }
}*/
  1. /*public class MainClassKt {
  2. public static void main(String[] args) {
  3. public static int sumNumbers(int value, int value2) {
  4. return value + value2;
/*public class MainClassKt {
    public static void main(String[] args) {

    }

    public static int sumNumbers(int value, int value2) {
        return value + value2;

    }
}*/

You can give a custom name in place of using the file name. The updated Kotlin file. Just wrote @file:JvmName("CustomKoltinClass") on the top of the file.

@file:JvmName("CustomKoltinClass")

fun main(args: Array<String>) {

}

fun sumNumbers(value: Int, value2: Int): Int {
    return value + value2

}
  1. @file:JvmName("CustomKoltinClass")
  2. fun main(args: Array<String>) {
  3. fun sumNumbers(value: Int, value2: Int): Int {
  4. return value + value2
@file:JvmName("CustomKoltinClass")

fun main(args: Array<String>) {

}

fun sumNumbers(value: Int, value2: Int): Int {
    return value + value2

}

And the updated Java class. Just changed the default Kotlin file name from MainClassKt to CustomKoltinClass

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

        int sum = CustomKoltinClass.sumNumbers(10, 12);
        System.out.println("The sum returned from Kotlin's sumNumbers function is " + sum);
    }
}
  1. public class CustomJavaClass {
  2. public static void main(String[] args) {
  3. int sum = CustomKoltinClass.sumNumbers(10, 12);
  4. System.out.println("The sum returned from Kotlin's sumNumbers function is " + sum);
public class CustomJavaClass {
    public static void main(String[] args) {

        int sum = CustomKoltinClass.sumNumbers(10, 12);
        System.out.println("The sum returned from Kotlin's sumNumbers function is " + sum);
    }
}

Again we have the same output it worked great. Now you know how to call Kotlin code from a Java class. Similar you can call Java code from a Kotlin class.

If you want to learn more about it please check if the official documentation.

</div


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK