Kotlin Q & A
How do you declare a nullable variable in Kotlin?
To declare a nullable variable in Kotlin, you append a question mark (?) to the type declaration. This indicates that the variable can hold a value of the specified type or be null. Here’s an example of declaring a nullable variable:
kotlin
var nullableString: String? = null
In this example, nullableString is declared as a nullable string type, allowing it to store either a valid string value or be null.
Nullable variables are particularly useful when dealing with situations where the absence of a value is a valid state, such as optional parameters, uninitialized variables, or database queries that may not return a result.
Previously at
Experienced Android Engineer specializing in Kotlin with over 5 years of hands-on expertise. Proven record of delivering impactful solutions and driving app innovation.