Kotlin Tutoial

Basics

Control Flow

Array & String

Functions

Collections

OOPs Concept

Exception Handling

Null Safety

Regex & Ranges

Java Interoperability

Miscellaneous

Android

Java Interoperability - Calling Kotlin from Java

Just as you can call Java from Kotlin, you can also call Kotlin from Java. This makes gradual adoption of Kotlin in Java projects a breeze. Let's delve into how to call Kotlin code from Java.

1. Setting Up:

Assuming you're using Android Studio or IntelliJ IDEA, having a mixed Java-Kotlin project is straightforward. You can add both Java and Kotlin files in the same project.

2. Simple Kotlin Class:

First, create a simple Kotlin class named SimpleKotlinClass.kt:

class SimpleKotlinClass {

    companion object {
        @JvmStatic
        fun sayHello(): String {
            return "Hello from Kotlin!"
        }
    }

    fun add(a: Int, b: Int): Int {
        return a + b
    }
}

3. Calling Kotlin from Java:

Now, create a Java file named MainJava.java:

public class MainJava {
    public static void main(String[] args) {
        // Calling a static method from a Kotlin class
        String message = SimpleKotlinClass.sayHello();
        System.out.println(message);
        
        // Creating an instance of the Kotlin class and calling an instance method
        SimpleKotlinClass kotlinInstance = new SimpleKotlinClass();
        int sum = kotlinInstance.add(5, 3);
        System.out.println("Sum: " + sum);
    }
}

4. Key Notes on Kotlin-Java Interoperability:

  1. Companion Object Functions: To call Kotlin static functions from Java, you need to annotate them with @JvmStatic in the companion object. Without the annotation, you'd need to call them like SimpleKotlinClass.Companion.sayHello().

  2. Property Access: Kotlin properties can be accessed in Java through their corresponding getter and setter methods. For example, a Kotlin property var name: String can be accessed in Java via getName() and setName(String) methods.

  3. Kotlin File Functions: If you have top-level functions in a Kotlin file (not inside a class), they're compiled to static methods in a Java class named after the Kotlin file. For instance, functions in Utils.kt would be accessible in Java via the UtilsKt class.

  4. Null Safety: Since Java doesn't have built-in null safety like Kotlin, you should be cautious when working with Kotlin objects that expect non-nullable types.

  5. Extensions: Kotlin's extension functions are compiled to static methods. So, if you have an extension function on a String class in Kotlin, it will be accessible as a static method with the String instance as the first parameter.

  6. @JvmOverloads: Kotlin supports default arguments for functions. However, Java doesn't. To invoke a Kotlin function with default arguments from Java, you can use the @JvmOverloads annotation. This generates multiple overloads for the Java side.

5. Conclusion:

Kotlin's ability to interoperate with Java is not one-sided. It's designed to be a two-way street, facilitating its adoption in existing Java projects. With this tutorial, you should be equipped to integrate Kotlin modules or utilities into your Java codebase. Remember, the real power of this interoperability shines when you gradually refactor or add features using Kotlin in a predominantly Java codebase, letting you enjoy the best of both worlds.

  1. Kotlin and Java interoperability examples: Kotlin is designed to be interoperable with Java seamlessly. You can use Kotlin code in Java projects without any issues.

  2. Calling Kotlin functions from Java code:

    // Java code calling a Kotlin function
    KotlinClassKt.kotlinFunction();
    
  3. Working with Kotlin classes in Java:

    // Java code using a Kotlin class
    KotlinClass kotlinObject = new KotlinClass();
    kotlinObject.kotlinMethod();
    
  4. Accessing Kotlin properties and functions in Java:

    // Java code accessing Kotlin properties and functions
    KotlinClass kotlinObject = new KotlinClass();
    String propertyValue = kotlinObject.getKotlinProperty();
    kotlinObject.setKotlinProperty("New Value");