-4
def Abc(){
    return this.abc?.equals(bcd.toString()) 
}

Also, trying to convert this to Java.

abc is a String. I understand that the '?' operator would do a null check and 'abc' it is not null it would go ahead and execute the equals part and return true or false on the basis of it, but what if it finds a null?

1 Answers1

0

Think of it as a short hand for the following:

return this.abc == null ? null : this.abc.equals(bdc.toString())

According to the Groovy Language Documentation

The Safe Navigation operator is used to avoid a NullPointerException. Typically when you have a reference to an object you might need to verify that it is not null before accessing methods or properties of the object. To avoid this, the safe navigation operator will simply return null instead of throwing an exception

So it will return null instead of false.

Joe Degiovanni
  • 205
  • 4
  • 6
  • After reading it out, I guess this is the interpretation in java : ( this.abc != null && this.abc.equals(bcd.toString()) ) – Aayush Shrivastava Apr 04 '18 at 14:11
  • Hmm, you're absolutely right !! But if I change this piece of code into java, I would want the return type of the method to be boolean, in which case, I won't be able to return a null, neither I would want it to throw a null pointer. Therefore, the java interpretation that would help here, would be the one I posted! – Aayush Shrivastava Apr 05 '18 at 12:21
  • Yes, if you want to return a boolean then you should use the code that you suggested. However, your original question was regarding what will Groovy return if the Safe Navigation operator returns `null` – Joe Degiovanni Apr 05 '18 at 17:12