🐦 Kotlin Beginner

How do you declare a nullable variable in Kotlin?

Answer

By default, all types in Kotlin are non-nullable — you cannot assign null to a regular String. To allow null, append a ? to the type: var name: String? = null. The Kotlin compiler enforces null safety at compile time — you cannot call methods on a nullable variable without first handling the null case, either with a null check, the safe call operator ?., the Elvis operator ?:, or the non-null assertion operator !!. This design eliminates most NullPointerExceptions at compile time.