Null Safety in Java and Kotlin
Null values are one of the most common causes of runtime crashes on the JVM.
Java allows null freely, while Kotlin was designed to
eliminate NullPointerException at compile time.
This topic is extremely important for real-world development, JVM understanding, and interviews.
Table of Contents
1. The Null Problem
A null reference means a variable points to no object.
Accessing methods or properties on a null reference causes
NullPointerException.
2. Null Handling in Java
In Java, any reference type can be null. The compiler does not enforce null checks.
Java Example
String name = null;
System.out.println(name.length()); // NullPointerException
To avoid crashes, developers must manually add null checks everywhere.
if (name != null) {
System.out.println(name.length());
}
Java 8 introduced Optional, but it is not enforced.
3. Null Safety in Kotlin
Kotlin separates types into:
- Non-nullable (
String) - Nullable (
String?)
By default, variables cannot hold null.
Kotlin Example
val name: String = "Kotlin"
// name = null // Compilation error
Nullable Type
val name: String? = null
The compiler forces you to handle null safely before usage.
Platform Types (Java Interoperability)
When Kotlin interacts with Java code, nullability information may be missing. Such types are called platform types.
// Java
String getName();
// Kotlin
val name = getName() // Can be String or String?
Platform types bypass Kotlin’s null safety and can cause runtime
NullPointerException if misused.
4. Safe Calls & Elvis Operator
Safe Call Operator (?.)
val length = name?.length
If name is null, the expression returns null instead of crashing.
Elvis Operator (?:)
val length = name?.length ?: 0
Provides a default value when the expression is null.
5. Not-null Assertion (!!)
The !! operator tells the compiler:
“Trust me, this is not null.”
val length = name!!.length
⚠️ If the value is null, Kotlin throws
NullPointerException.
Interview Tip: Excessive use of !! is considered bad practice.
6. Java and Kotlin – Null Safety Comparison
| Aspect | Java | Kotlin |
|---|---|---|
| Null allowed | Everywhere | Only with ? |
| Compile-time safety | No | Yes |
| Runtime crashes | Common | Rare |
| Developer effort | Manual checks | Compiler enforced |
7. Interview Questions & Answers
Q1. What is NullPointerException?
Answer: It occurs when accessing a method or property on a null reference.
Q2. How does Kotlin prevent NullPointerException?
Answer: By separating nullable and non-nullable types and enforcing checks at compile time.
Q3. What is the purpose of the ? operator?
Answer: It marks a variable as nullable.
Q4. What does ?. do?
Answer: Performs a safe call and returns null if the object is null.
Q5. What is the Elvis operator?
Answer: It provides a default value when an expression evaluates to null.
Q6. Is !! safe?
Answer: No. It can throw NullPointerException.
Q7. Does Kotlin completely eliminate null?
Answer: No. It controls and restricts null usage.
Q8. How does Java Optional differ from Kotlin null safety?
Answer: Optional is a library feature; Kotlin null safety is a language feature.
Q9. Can Kotlin code throw NullPointerException?
Answer: Yes, through !!, Java interop, or platform types.
Q10. Why is null safety important in JVM applications?
Answer: It reduces crashes, improves reliability, and simplifies code.
8. Conclusion
Null safety is one of Kotlin’s most powerful features. It shifts error detection from runtime to compile time, making applications safer and easier to maintain.
At runtime, both Java and Kotlin rely on the JVM, but Kotlin shifts null checks to compile time, reducing runtime failures.
Understanding null handling is critical for:
- Writing crash-free JVM applications
- Working with Java-Kotlin interop
- Clearing technical interviews
Comments
Post a Comment