Groovy:如果安全导航操作符在原语赋值时返回null,会发生什么?

Groovy:如果安全导航操作符在原语赋值时返回null,会发生什么?,groovy,null,int,variable-assignment,primitive,Groovy,Null,Int,Variable Assignment,Primitive,你会得到: Character parrot = null if (parrot?.hp < 0) { print (parrot+" is pining for the fjords.") } 对。正如您所说,null

你会得到:

Character parrot = null
if (parrot?.hp < 0) {
    print (parrot+" is pining for the fjords.")
}
  • 对。正如您所说,
    null<0
    因此您得到了输出:

    org.codehaus.groovy.runtime.typehandling.GroovyCastException:
         Cannot cast object 'null' with class 'null' to class 'int'.
         Try 'java.lang.Integer' instead
    

  • 你试过了吗?或者这是一个反问句?
    org.codehaus.groovy.runtime.typehandling.GroovyCastException:
         Cannot cast object 'null' with class 'null' to class 'int'.
         Try 'java.lang.Integer' instead
    
    null is pining for the fjords