Junior — Middle
Explain what is meant by using a question mark type in the context of typing.
sobes.tech AI
Answer from AI
In the context of type system in Android (specifically in Kotlin), using a question mark ? after a type indicates that the variable can hold a null value. Such a type is called nullable.
For example:
var name: String? = null
Here, name can contain a string or be null. This helps the compiler track potential null-related errors and forces the developer to handle such cases explicitly using safe calls (?.), Elvis operator (?:), and other mechanisms.
Without ?, the type is considered non-nullable, and the variable cannot be null.