Platform Types
Nullability from Java.
Platform Types is a free Kotlin Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Kotlin Meets Java
Kotlin runs on the JVM and calls Java code constantly. But Java has no concept of nullable versus non-null types.
So how does Kotlin handle a value coming from Java? Through platform types.
What Is a Platform Type
A platform type is a type Kotlin gets from Java whose nullability is unknown.
Kotlin writes it as String! in error messages. The exclamation means: could be null, could be non-null, Kotlin will not enforce a choice.
Relaxed Checking
For platform types Kotlin relaxes its null checks. You can treat the value as non-null without a compiler error.
The trade-off: if it really is null, you get a runtime NullPointerException.
You Choose the Type
When you store a platform-type value, you decide how to type it:
- as non-null
String→ convenient but risky - as nullable
String?→ safe and explicit
This Kotlin example simulates that choice.
fun fromJava(): String? = "value from Java"
fun main() {
val safe: String? = fromJava()
println(safe?.length)
}The Risky Choice
If you assume non-null and the source returns null, the crash happens at the assignment or first use.
This is the hidden danger of platform types.
fun fromJava(): String? = null
fun main() {
try {
val risky: String = fromJava()!!
println(risky.length)
} catch (e: NullPointerException) {
println("Null came from the Java side")
}
}Annotations Help
Java libraries can use annotations like @Nullable and @NotNull.
When present, Kotlin honors them and gives you a proper String? or String instead of a platform type.
Best Practice: Assume Nullable
When calling unannotated Java code, the safe habit is to treat the result as nullable.
Type your variable with ? and use safe calls, just like any Kotlin nullable.
fun fromJava(): String? = "data"
fun main() {
val value: String? = fromJava()
val length = value?.length ?: 0
println(length)
}Defensive Boundaries
Treat the Java boundary as untrusted. Validate or default values right where they enter your Kotlin code.
After that point, the rest of your code works with clean Kotlin types.
fun fromJava(): String? = null
fun main() {
val name = fromJava() ?: "default"
// from here on, name is a clean non-null String
println(name.uppercase())
}Why Kotlin Allows This
Kotlin could force every Java value to be nullable, but that would make Java interop painfully verbose.
Platform types are a pragmatic compromise: convenience plus the option to be strict.
Spotting Platform Types
You will not write String! yourself. It only appears in IDE hints and error messages when you hover over Java-sourced values.
Seeing the ! is your cue to decide on nullability deliberately.
Putting It Together
Platform types bridge Java and Kotlin:
- They have unknown nullability, shown as
Type! - Kotlin lets you choose nullable or non-null
- Assume nullable when unsure to stay safe
- Annotations remove the guesswork
fun fromJava(): String? = "safe value"
fun main() {
val value: String? = fromJava()
println(value?.uppercase() ?: "missing")
}Quick Check
Test your understanding of platform types.
Recap
You learned how nullability crosses the Java boundary:
- Platform types have unknown nullability
- Kotlin relaxes checks but a wrong assumption causes a runtime NPE
- Assume nullable and default values at the boundary
- Java annotations restore proper types
That completes the Null Safety Deep Dive.
Frequently asked questions
Is the “Platform Types” lesson free?
Yes — the full text of “Platform Types” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.
What will I learn in “Platform Types”?
Nullability from Java. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Academy?
No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Platform Types” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Academy lesson?
Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.