DevOps / Apache Groovy Interview questions
Define the safe navigation operator in Groovy?
The safe navigation operator, written ?., lets you call a method or access a property on an object that might be null without throwing a NullPointerException - if the object is null, the whole expression short-circuits and simply evaluates to null instead of throwing.
It's especially useful for chained property access, like user?.address?.city, where any link in the chain could be null; without it, each step would need an explicit null check to avoid an exception.
It only guards against the object immediately to its left being null - a longer chain still needs ?. applied at each link where a null is genuinely possible, since the check doesn't automatically extend past where it's applied.
More Related questions...