📖 Scenario: You are working on a project where Kotlin and Java code need to work together. You want to call a Kotlin function from Java code.
🎯 Goal: Build a simple Kotlin function and call it from Java code, then print the result.
📋 What You'll Learn
Create a Kotlin file with a function named greet that takes a String parameter called name and returns a greeting message.
Create a Java class with a main method that calls the Kotlin greet function with the name "Alice".
Print the greeting message returned by the Kotlin function in the Java main method.
💡 Why This Matters
🌍 Real World
Many Android apps and JVM projects use both Kotlin and Java together. Knowing how to call Kotlin from Java helps you work on mixed codebases.
💼 Career
Understanding interoperability between Kotlin and Java is important for Android developers and backend developers working on JVM platforms.
Progress0 / 4 steps
1
Create Kotlin function greet
Create a Kotlin file named Greeting.kt and write a function called greet that takes a String parameter name and returns the string "Hello, " plus the name.
Kotlin
Hint
Use a Kotlin function with a String parameter and return a greeting using string templates.
2
Create Java class with main method
Create a Java class named Main with a main method. Inside the method, declare a String variable named name and set it to "Alice".
Kotlin
Hint
Write a Java class with a main method and declare a String variable.
3
Call Kotlin greet function from Java
In the Java main method, call the Kotlin function greet with the variable name and store the result in a String variable called message.
Kotlin
Hint
Use the Kotlin file name GreetingKt to call the greet function from Java.
4
Print the greeting message in Java
In the Java main method, print the message variable using System.out.println.
Kotlin
Hint
Use System.out.println(message); to print the greeting.
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
Step 1: Understand Kotlin-Java interoperability
Kotlin classes compile to Java bytecode, so their functions appear as normal Java methods.
Step 2: Confirm Java can call Kotlin class functions
Java code can create Kotlin objects and call their methods just like Java methods.
Final Answer:
You can call Kotlin class functions like normal Java methods. -> Option A
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
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.
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().
Final Answer:
KotlinFileKt.greet(); -> Option A
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
Step 1: Identify the Kotlin function behavior
The Kotlin function adds two integers and returns the sum.
Step 2: Understand Java call to Kotlin top-level function