Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Kotlin
Stage 5: Specialization

Part 03 / Topic 1 of 8

Nullable vs non-null types

Every type in Kotlin comes in two forms: a plain type like String, which can never hold null, and that same type with a ? suffix, String?, which can hold either a String or null. This is not a convention or a naming style — it's enforced by the compiler at every single point a value of that type is used.

val city: String = "Pune"              // non-null: this variable can never hold null
val pincode: String? = pincodes[city]  // nullable: Map.get() returns String?, the key might be missing

Map.get(key) (the [...] you've already used) is a good example of why nullable types exist: the key you ask for might not be in the map, and Kotlin's standard library says so honestly in the return type, String?, instead of returning null from a method that claims to return String the way plenty of Java APIs do.

What happens when you ignore the ?

Two mistakes, both real compile errors, both run for real on this machine:

$ kotlinc ProfileDraft.kt -d outdraft
ProfileDraft.kt:4:21: error: only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type 'String?'.
    println(nickname.length)
                    ^

ProfileDraft.kt declared nickname as String? and then called .length on it directly, no ?., no !!. The compiler stops the build — this line can never run, because the compiler cannot prove nickname isn't null at that point.

$ kotlinc ProfileDraft3.kt -d outdraft3
ProfileDraft3.kt:3:12: error: null cannot be a value of a non-null type 'String'.
    city = null
           ^^^^

The opposite mistake: city was declared String (no ?), so assigning null to it is rejected before the program even compiles, let alone runs. Compare this to the equivalent Java mistake — String city = null; compiles without a single warning in Java, and the bug only shows up the day something calls city.length(). Kotlin refuses to let that gap exist.

Smart casts: narrowing a nullable type inside a check

Once you've actually checked that a nullable value isn't null, Kotlin lets you use it as the plain, non-null type for the rest of that branch — no ?., no !!, nothing extra. This is called a smart cast, and the compiler does it for you automatically:

if (pincode != null) {
    // Inside this block the compiler treats pincode as String, not String?,
    // so .length is allowed with no ?. and no !!.
    println("pincode has ${pincode.length} digits")
} else {
    println("pincode is not set")
}

Smart casts only work on a val, or on a var the compiler can prove nothing else touched between the check and the use (a local var in a simple function usually qualifies; a mutable property of a class, revisited in Part 7, usually doesn't, because another thread or another method could have changed it in between). Sections 2–4 below cover the shorter, more idiomatic way to write the same idea without an explicit if.

$ kotlinc 01_nullable_types.kt -include-runtime -d 01.jar
$ java -jar 01.jar
city = Pune
pincode = 411001
pincode has 6 digits
Hyderabad is not in the pincodes map

Full program: examples/01_nullable_types.kt.

Done here? The practice set for Part 3 — including the solutions — is on the part page.

Search the guide