Did You Know In Kotlin Programming Language Null Variables Are Not Allowed By Default?

Computers & Internet

Did You Know In Kotlin Programming Language Null Variables Are Not Allowed By Default?

In Kotlin you can't declare a null variable. You have to do appropriate housework before doing such things. Kotlin provides builtin nullable operators for handling the situation.


A variable of type String can not hold null data. It will throw an error if you ever try to do so. Please don't try this at home. Lol


Let's try to run this code.


fun main() {

// Kotlin don't allow nullified variables.
// In Kotlin you can't declare a null variable.
// You've to take care of somethings before attempting such heavy thing.
// Let's experiment this by trying to assign the value to null.
var tryingToSetANullVariableInKotlin: Int = null

print(tryingToSetANullVariableInKotlin)
}

It will throw this kind of error: "Null can not be a value of a non-null type Int."