Bird
Raised Fist0
Kotlinprogramming~10 mins

Calling Kotlin from Java - Step-by-Step Execution

Choose your learning style10 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.

Practice

(1/5)
1. Which statement is true about calling Kotlin functions from Java?
easy
A. You can call Kotlin class functions like normal Java methods.
B. You cannot call Kotlin functions from Java directly.
C. Kotlin functions must be rewritten in Java to be used.
D. Kotlin functions are only accessible in Kotlin files.

Solution

  1. Step 1: Understand Kotlin-Java interoperability

    Kotlin classes compile to Java bytecode, so their functions appear as normal Java methods.
  2. Step 2: Confirm Java can call Kotlin class functions

    Java code can create Kotlin objects and call their methods just like Java methods.
  3. Final Answer:

    You can call Kotlin class functions like normal Java methods. -> Option A
  4. Quick Check:

    Kotlin class functions = callable from Java [OK]
Hint: Kotlin classes compile to Java classes, so methods are callable [OK]
Common Mistakes:
  • Thinking Kotlin functions need rewriting in Java
  • Believing Kotlin functions are inaccessible from Java
  • Confusing top-level functions with class functions
2. Which is the correct way to call a top-level Kotlin function fun greet() from Java?
easy
A. KotlinFileKt.greet();
B. greet();
C. new KotlinFile().greet();
D. Kotlin.greet();

Solution

  1. Step 1: Understand top-level function compilation

    Top-level Kotlin functions compile into static methods inside a generated class named after the Kotlin file with 'Kt' suffix.
  2. Step 2: Call the function from Java using generated class

    Java calls the function as a static method on the generated class, e.g., KotlinFileKt.greet().
  3. Final Answer:

    KotlinFileKt.greet(); -> Option A
  4. Quick Check:

    Top-level function call = KotlinFileKt.greet() [OK]
Hint: Top-level Kotlin functions appear in Java as static methods in FileNameKt class [OK]
Common Mistakes:
  • Trying to call top-level function directly without class
  • Assuming top-level functions need an instance
  • Using wrong class name without 'Kt' suffix
3. Given Kotlin file Utils.kt with top-level function:
fun add(a: Int, b: Int): Int = a + b

What is the output of this Java code?
System.out.println(UtilsKt.add(3, 4));
medium
A. Runtime exception
B. 34
C. Compile error
D. 7

Solution

  1. Step 1: Identify the Kotlin function behavior

    The Kotlin function adds two integers and returns the sum.
  2. Step 2: Understand Java call to Kotlin top-level function

    Java calls UtilsKt.add(3, 4), which returns 3 + 4 = 7.
  3. Final Answer:

    7 -> Option D
  4. Quick Check:

    3 + 4 = 7 [OK]
Hint: Top-level Kotlin functions return expected results when called from Java [OK]
Common Mistakes:
  • Confusing concatenation with addition
  • Expecting compile error due to Kotlin-Java call
  • Assuming runtime exception from Kotlin code
4. What is wrong with this Java code calling Kotlin class Person with function fun greet()?:
Person p = new Person();
p.greet;
medium
A. greet is a private function in Kotlin.
B. Person class cannot be instantiated in Java.
C. Missing parentheses to call the function.
D. Kotlin functions cannot be called from Java.

Solution

  1. Step 1: Check Java method call syntax

    In Java, methods must be called with parentheses, even if no arguments.
  2. Step 2: Identify the error in code

    Code uses p.greet; missing parentheses, causing syntax error.
  3. Final Answer:

    Missing parentheses to call the function. -> Option C
  4. Quick Check:

    Method call syntax requires () [OK]
Hint: Always use parentheses to call methods in Java [OK]
Common Mistakes:
  • Omitting parentheses when calling methods
  • Assuming Kotlin classes can't be instantiated in Java
  • Thinking Kotlin functions are private by default
5. You have a Kotlin file MathUtils.kt with:
@JvmStatic
fun multiply(a: Int, b: Int): Int = a * b

How should you call multiply from Java to get 20 when multiplying 4 and 5?
hard
A. MathUtilsKt.multiply(4, 5);
B. MathUtils.Companion.multiply(4, 5);
C. new MathUtils().multiply(4, 5);
D. MathUtils.multiply(4, 5);

Solution

  1. Step 1: Understand @JvmStatic effect on Kotlin functions

    @JvmStatic makes the function a static method inside the companion object, callable via the Companion instance.
  2. Step 2: Identify correct Java call

    Java calls MathUtils.Companion.multiply(4, 5) to get 20.
  3. Final Answer:

    MathUtils.Companion.multiply(4, 5); -> Option B
  4. Quick Check:

    @JvmStatic function call = MathUtils.Companion.multiply() [OK]
Hint: @JvmStatic makes Kotlin function callable as static method in Java companion object [OK]
Common Mistakes:
  • Calling via generated Kt class ignoring @JvmStatic
  • Trying to instantiate Kotlin object unnecessarily
  • Calling via Companion object when not needed
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard