Kotlin Tutoial
Basics
Control Flow
Array & String
Functions
Collections
OOPs Concept
Exception Handling
Null Safety
Regex & Ranges
Java Interoperability
Miscellaneous
Android
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.
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.
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 } }
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); } }
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()
.
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.
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.
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.
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.
@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.
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.
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.
Calling Kotlin functions from Java code:
// Java code calling a Kotlin function KotlinClassKt.kotlinFunction();
Working with Kotlin classes in Java:
// Java code using a Kotlin class KotlinClass kotlinObject = new KotlinClass(); kotlinObject.kotlinMethod();
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");