Java Interop
In this page:
Calling Java Standard Library Classes
Kotlin can use any Java class directly, including the entire Java standard library, since Kotlin compiles down to the same JVM bytecode Java does.
Example: Calling Java Standard Library Classes
import java.util.ArrayList
fun main() {
val javaList = ArrayList<String>()
javaList.add("Kotlin")
javaList.add("Java")
println(javaList)
}
Login to try C/C++/Java/PHP code in the editor
Platform Types from Java
A value coming from Java code with no nullability annotation is treated by Kotlin as a 'platform type', shown as String! internally, which skips strict compile-time null checking until you decide how to treat it.
Note: Always add an explicit null check around values coming from Java APIs without nullability annotations.
Example: Platform Types from Java
fun getJavaStyleValue(): String? {
// Simulating a Java method with unknown nullability
val value: String? = if (System.currentTimeMillis() % 2 == 0L) "value" else null
return value
}
fun main() {
val result = getJavaStyleValue()
println("Result: ${result ?: "was null"}")
}
Login to try C/C++/Java/PHP code in the editor
Using Java Getters and Setters as Properties
Kotlin lets you call a Java class's getX()/setX() methods using property-style syntax, such as obj.x instead of obj.getX(), when calling from Kotlin.
Example: Using Java Getters and Setters as Properties
import java.util.Calendar
fun main() {
val calendar = Calendar.getInstance()
val year = calendar.get(Calendar.YEAR)
println("Current year (via Java API): $year")
}
Login to try C/C++/Java/PHP code in the editor
Kotlin Functions Called from Java
A top-level Kotlin function compiles into a static method on an automatically generated class named after its file (e.g. functions in Utils.kt become static methods on UtilsKt), which is how Java code calls them.
Example: Kotlin Functions Called from Java
fun greet(name: String): String = "Hello, $name!"
fun main() {
// Java code would call this as UtilsKt.greet("Kotlin")
println(greet("Kotlin"))
}
Login to try C/C++/Java/PHP code in the editor
- Forgetting that a Java method returning a plain (non-annotated) reference type appears to Kotlin as a 'platform type', which skips Kotlin's null-safety checks until you add your own.
- Assuming every Java naming convention carries over unchanged; Kotlin has some special call syntax for Java getters/setters (property syntax) that Java methods don't have natively.
- Not realizing a Kotlin top-level function compiles to a static method on an automatically generated class (like
FileNameKt) when called from Java.
- Kotlin code can call Java classes and methods directly, and Java code can call Kotlin classes and functions directly, since both compile to JVM bytecode.
- A Java method's return type with no nullability annotation becomes a 'platform type' in Kotlin, which bypasses compile-time null checks.
- Kotlin can access Java getter/setter pairs (
getX()/setX()) using property-style syntax (obj.x). - Kotlin top-level functions become static methods on a generated class named after the file when called from Java.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: