0
0
Kotlinprogramming~10 mins

Calling Kotlin from Java - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Calling Kotlin from Java
Write Kotlin class/function
Compile Kotlin to JVM bytecode
Java code imports Kotlin class
Java calls Kotlin function/method
Kotlin code executes and returns result
Java receives and uses result
Java code can call Kotlin classes and functions compiled to JVM bytecode by importing and invoking them like regular Java classes.
Execution Sample
Kotlin
class Greeting {
    fun greet(name: String): String {
        return "Hello, $name!"
    }
}

// Java code:
// Greeting greeting = new Greeting();
// String message = greeting.greet("Alice");
This Kotlin class has a greet function that Java code can call to get a greeting message.
Execution Table
StepActionCode LocationResult/Output
1Kotlin class Greeting compiledKotlin fileGreeting class available in JVM bytecode
2Java imports Greeting classJava fileGreeting class recognized by Java compiler
3Java creates Greeting instanceJava fileGreeting object created
4Java calls greet("Alice")Java fileCalls Kotlin greet function with "Alice"
5Kotlin greet executesKotlin classReturns "Hello, Alice!"
6Java receives return valueJava filemessage = "Hello, Alice!"
7Java uses messageJava filePrints or processes greeting message
8End-Java successfully called Kotlin function
💡 Execution stops after Java receives and uses the Kotlin function's return value.
Variable Tracker
VariableStartAfter Step 3After Step 6Final
greetingnullGreeting instanceGreeting instanceGreeting instance
nameN/AN/A"Alice"N/A
messagenullnull"Hello, Alice!""Hello, Alice!"
Key Moments - 3 Insights
How does Java recognize the Kotlin class Greeting?
Because Kotlin compiles to JVM bytecode, the Greeting class is available to Java like any Java class, as shown in step 2 of the execution table.
Can Java call Kotlin functions directly without any special syntax?
Yes, Java calls Kotlin functions like normal Java methods, as seen in step 4 where Java calls greet("Alice").
What happens to Kotlin's string interpolation when called from Java?
Kotlin's string interpolation is resolved inside Kotlin before returning, so Java just receives the final string, as in step 5 returning "Hello, Alice!".
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'message' after step 6?
A"Alice"
B"Hello, Alice!"
Cnull
DGreeting instance
💡 Hint
Check the 'Result/Output' column at step 6 where Java receives the return value.
At which step does Java create an instance of the Kotlin class?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the 'Action' column for object creation in the execution table.
If Kotlin function greet returned an integer instead of a string, how would the Java variable 'message' change?
AIt would still hold a string
BIt would be null
CIt would hold an integer value
DJava cannot receive integers from Kotlin
💡 Hint
Refer to variable_tracker and understand that Java receives the Kotlin return value as is.
Concept Snapshot
Calling Kotlin from Java:
- Kotlin compiles to JVM bytecode.
- Java imports Kotlin classes like Java classes.
- Java creates Kotlin objects and calls functions normally.
- Kotlin functions return values Java can use directly.
- Kotlin features like string interpolation resolve before Java sees results.
Full Transcript
This visual execution shows how Java calls Kotlin code. First, Kotlin classes compile to JVM bytecode. Java imports these classes and creates objects. Then Java calls Kotlin functions like normal methods. The Kotlin function executes and returns a result, which Java receives and uses. Variables like the greeting message change as the call progresses. This process lets Java and Kotlin work together smoothly on the JVM.