tezvyn:

Explain val vs var in Kotlin and null safety risks

Curated by the Tezvyn teamSource: kotlinlang.orgbeginner
Explain val vs var in Kotlin and null safety risks
WHAT IT TESTS

Kotlin mutability and compile-time null safety.

ANSWER OUTLINE

val is read-only, var is mutable; nullables use ? like String?; risk is NullPointerException if checks are missed.

RED FLAG

Calling val deep immutability or defaulting to !!.

WHAT THIS TESTS: This question checks whether you understand two foundational Kotlin type-system concepts: reference mutability and null safety. The interviewer wants to see that you know val controls reassignment of the reference, not the underlying object state, and that you treat nullability as an explicit type-level contract rather than an afterthought.

A GOOD ANSWER COVERS four things in order. First, define val as a read-only reference that cannot be reassigned once initialized, while var is a mutable reference that can be changed. Second, clarify that val does not guarantee deep immutability; if the object is a mutable list or a class with var properties, its contents can still change. Third, explain that a nullable variable is declared by appending a question mark to the type, such as String?, and that the compiler then forces you to handle the null case before accessing members. Fourth, state that the main risk of nullable types is NullPointerException if you bypass safety with the not-null assertion operator or fail to check for null, and that nullability tends to propagate through call chains, increasing cognitive overhead and defensive code.

COMMON WRONG ANSWERS include saying val makes an object deeply immutable, which is false because a val holding a MutableList can still have items added. Another red flag is claiming Kotlin eliminates all NullPointerExceptions; in reality NPEs can still occur through explicit throws, the !! operator, leaking this during initialization, or Java interop with platform types. A third mistake is recommending the !! operator as a normal fix; senior candidates should prefer safe calls, Elvis operators, or early returns.

LIKELY FOLLOW-UPS include asking when to choose val over var, which should be answered with prefer val by default for readability and thread-safety of references. The interviewer may also ask how to handle nullable collections, or how lateinit and nullable types differ, or how Kotlin null safety maps to Java code via platform types and annotations.

ONE CONCRETE EXAMPLE: Declare a non-nullable user name with val name: String = Ada, which cannot be reassigned. Declare a nullable nickname with var nickname: String? = null. Accessing nickname.length fails to compile, so you must use nickname?.length ?: 0 to safely get a default length. If you instead write nickname!!.length, you risk a NullPointerException at runtime when nickname is null.

Source: kotlinlang.org

Read the original → kotlinlang.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.

Explain val vs var in Kotlin and null safety risks · Tezvyn