Interview QuestionPractical QuestionFollow-up Questions

Null Plus Null in Kotlin

skydovesJaewoong Eum (skydoves)||8 min read

Null Plus Null in Kotlin

Kotlin handles null values with a null safety system, but some behaviors around null can still surprise developers. One common interview topic is what happens when you evaluate null + null. The result is not a compiler error or a NullPointerException, but the string "nullnull". Understanding why this happens requires examining how Kotlin resolves the + operator and how null interacts with the String.plus() extension function. By the end of this lesson, you will be able to:

  • Explain why null + null produces the string "nullnull" instead of a compiler error.
  • Describe how Kotlin resolves the + operator through String.plus().
  • Identify how null.toString() converts null into the string "null".
  • Recognize other operator behaviors involving null that may produce unexpected results.

How the Plus Operator Resolves on Null

When Kotlin encounters null + null, it looks for an applicable plus function. The Kotlin standard library defines an extension function on Any? for string concatenation:

public operator fun Any?.plus(other: Any?): String {
    return toString() + other.toString()
}

Because Any? includes null, this extension function matches when the left operand is null. The compiler selects this overload and generates a call to it. The expression null + null becomes null.plus(null) at the call site.

How null.toString() Works

The toString() function defined on Any? handles null explicitly. When the receiver is null, it returns the string literal "null":

public fun Any?.toString(): String {
    return this?.toString() ?: "null"
}

So when null.plus(null) executes, the first toString() call on the receiver returns "null", and the second toString() call on the argument also returns "null". The two strings are concatenated, producing "nullnull".

val result = null + null
println(result)        // Output: nullnull
println(result.length) // Output: 8

The return type of this expression is String, not String?. The compiler infers this from the return type of the plus extension function.

String Concatenation with Null Operands

This behavior extends to any combination involving null and the + operator in a string context. When one operand is a String and the other is null, the same toString() conversion applies:

val a: String? = null
val b = "hello"
println(a + b)   // Output: nullhello
println(b + a)   // Output: hellonull

Both cases produce a String result because the plus function always calls toString() on each operand. The null value becomes "null" in every case. This is consistent with how Java's String.valueOf(null) returns "null", but it can lead to unintended data corruption if developers concatenate nullable values without checking them first.

This interview continues for subscribers

Subscribe to Dove Letter for full access to exclusive interviews about Android and Kotlin development.

Become a Sponsor