The safe navigation operator (?.) can be used to replace explicit, sequential checks for null references. This new operator short-circuits expressions that attempt to operate on a null value and returns null instead of throwing a NullPointerException.
If the left-hand side of the chain expression evaluates to null, the right-hand side is not evaluated. Use the safe navigation operator (?.) in method, variable, and property chaining. The part of the expression that is not evaluated can include variable references, method references, or array expressions.
Following example first evaluates a, and returns null if a is null. Otherwise the return value is a.b
a?.b // Evaluates to: a == null ? null : a.b
One thought on “Safe Navigation Operator”