what is the ? on groovy variables?
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.
精彩评论