Junior — Middle
How do you understand the concept of null-safety in the Kotlin language and why is it important in application development?
sobes.tech AI
Answer from AI
In Kotlin, the concept of null-safety means that the language natively supports control over the possibility of variables taking the value null. Variables by default cannot be null unless explicitly specified with a ? type, for example, String?. This helps to avoid common errors like NullPointerException, making the code more reliable and safe. For example:
var nonNullString: String = "Hello"
var nullableString: String? = null
// nonNullString = null // Compilation error
if (nullableString != null) {
println(nullableString.length) // Safe
}
Thus, null-safety improves code quality and reduces runtime errors.