Null Safety Java Interop in Kotlin
On this page (12sections)
Introduction
Null Safety Java Interop is a fundamental concept every Kotlin developer should understand. Kotlin and Java compile to the same JVM bytecode, so you can mix both languages in one project with minimal friction.
Java types are platform types in Kotlin with unknown nullability. In this tutorial you will learn the syntax, walk through a complete example program, study the sample output, and review best practices so you can apply the concept confidently in your own projects.
Definition
- Java types are platform types in Kotlin with unknown nullability.
- Treat platform types carefully or annotate Java with @Nullable/@NotNull.
- Use safe calls when Java may return null.
Syntax
val value = javaMethod() // platform type
Null Safety Java Interop in Kotlin Example Program in Kotlin
fun parseLength(input: String?): Int {
return input?.length ?: 0
}
fun main(args: Array<String>) {
println(parseLength("Kotlin"))
println(parseLength(null))
}
Sample Output
6
0
When to use
Use interop when migrating a Java codebase incrementally or calling mature Java libraries from Kotlin.
How it works
-
The program starts with a
mainfunction — the entry point that runs when you execute the file. -
return input?.length ?: 0demonstrates null-safe or type-safe Kotlin syntax in action. -
The
println(parseLength("Kotlin"))statement writes a line to the console — this produces part of the sample output below. -
The
println(parseLength(null))statement writes a line to the console — this produces part of the sample output below. -
Java types are platform types in Kotlin with unknown nullability.
-
Run the program in IntelliJ IDEA, Android Studio, or with the Kotlin command-line compiler (
kotlinc/kotlin). Compare your console output with the sample output shown below.
Best Practices
- Add
@JvmStatic,@JvmOverloads, or@JvmFieldwhen Java callers need a cleaner API. - Use
@file:JvmNameto avoid awkward Kotlin-generated class names in Java. - Be explicit about nullability with
@Nullable/@NonNullannotations for Java consumers.
Common Mistakes
- Skipping the example and only reading the definition — hands-on practice cements the concept.
- Copying syntax without understanding nullable vs non-nullable types or scope rules.
- Ignoring compiler warnings that often point to safer alternatives.
Key Points
- Java types are platform types in Kotlin with unknown nullability.
- Treat platform types carefully or annotate Java with @Nullable/@NotNull.
- Use safe calls when Java may return null.
- Test the example locally and verify the output matches the sample.
- Experiment by changing input values to see how behaviour changes.
Notes
- Nullability annotations help Kotlin and Java agree on which values can be null at boundaries.
- Semicolons at the end of statements are optional in Kotlin.
Frequently Asked Questions
What is Null Safety Java Interop in Kotlin?
When should I use Null Safety Java Interop?
How is Null Safety Java Interop different from Java?
How do I practice this topic?
Related Tutorials
Kotlin Properties in Java
Learn Kotlin Properties in Java in Kotlin with clear explanation, syntax, example program, sample output, best practices, and FAQs.
Read tutorialKotlin SAM Conversions in Kotlin
Learn Kotlin SAM Conversions in Kotlin with clear explanation, syntax, example program, sample output, best practices, and FAQs.
Read tutorial