0
0
Kotlinprogramming~30 mins

Calling Kotlin from Java - Mini Project: Build & Apply

Choose your learning style9 modes available
Calling Kotlin from Java
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use System.out.println(message); to print the greeting.