Kotlin Tips: What happens if you Add `null + null` in Kotlin

Birat Rai
Jun 9, 2023

This Kotlin Tips Series will include some useful tips in Kotlin based on Kotlin Tips 2023 by JetBrains.

Null-safety is number one in Kotlin — but what happens when you add null + null,

val result = null + null

Why does it return a string?

Kotlin tries to be helpful and tries to find operator overloading, which in this case is plus operator (+)

This only happens for null literals but not with null objects. With null objects IDE will try to suggest some errors.

Previous Tip

Next tips. Coming soon …..

References:

--

--