开发者

what is the ? on groovy variables?

开发者 https://www.devze.com 2023-01-27 04:52 出处:网络
I saw in some groovy code this: trip.id?.encodeAsH开发者_如何学PythonTML() What is the difference between using or not using \"id?.\"?It checks if the object is null or not.

I saw in some groovy code this:

trip.id?.encodeAsH开发者_如何学PythonTML()

What is the difference between using or not using "id?."?


It checks if the object is null or not. Using it, you can prevent nullpointer exception.

If you use it, you should use it for the whole object (eg: trip.id?.otherstuff?.morestuff?.encodeAsHTML()


It's called the "null-safe dereferencing operator". The difference is that if trip.id is null, instead of throwing a NullPointerException, it will return null as the result of the method call.


It's the Groovy null-safe operator. It performs a null check before dereferencing the object. See more on Groovy Operators here.

0

精彩评论

暂无评论...
验证码 换一张
取 消