Java Interop
In this page:
import java.util.ClassName
val javaObject = ClassName()
javaObject.method(argument)
javaObject.property = value // calls setProperty(...)
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`
import java.util.ArrayList
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `javaList`, set to `ArrayList<String>()`
val javaList = ArrayList<String>()
// Add `"Kotlin"` to `javaList`
javaList.add("Kotlin")
// Add `"Java"` to `javaList`
javaList.add("Java")
// Print `javaList` to the console, with a trailing newline
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.
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`
import java.util.Calendar
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `calendar`, set to `Calendar.getInstance()`
val calendar = Calendar.getInstance()
// Declare a read-only variable `year`, set to `calendar.get(Calendar.YEAR)`
val year = calendar.get(Calendar.YEAR)
// Print "Current year (via Java API): $year" to the console, with a trailing newline
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: