Discussion
Why does adding null + null produce a String?
Kotlin’s plus operator (+) can do different things—adding numbers, merging lists, joining strings, and so on—depending on the types of its inputs. Of these possible operations, it turns out that string concatenation is the only one that can accept a null value on both sides. That leaves Kotlin with no choice but to interpret null + null as an attempt to combine two nullable strings.
Anytime you use null in a string-building operation like this, Kotlin will replace it with the literal text "null". We’ve provided two null inputs, so we get two copies of the word "null", which are then stitched together just like any other pair of strings would be.
Operator Overloads
This is a fun puzzle, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access