Junior — Middle
What happens if you access an object declared as non-nullable using the '?' operator?
sobes.tech AI
Answer from AI
If an object is declared as non-nullable (for example, using annotations or in Kotlin without a nullable type), then using the safe call operator ?. on such an object is technically possible, but usually redundant.
The ?. operator is used to access properties or methods of an object that may be null, to avoid NullPointerException. If the object is guaranteed not to be null, then ?. behaves like a regular . operator — the call happens directly.
For example, in Kotlin:
val nonNullString: String = "Hello"
println(nonNullString?.length) // Works, but ? is unnecessary, result is 5
Thus, if an object cannot be null, the ?. operator will not change the behavior, but its use is meaningless and may indicate redundancy or a mistake in the code.