Facundo Ch.
Facundo Ch.

Reputation: 12234

what is the ? on groovy variables?

I saw in some groovy code this:

trip.id?.encodeAsHTML()

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

Upvotes: 3

Views: 343

Answers (3)

Michael Borgwardt
Michael Borgwardt

Reputation: 346526

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.

Upvotes: 4

Matt Lachman
Matt Lachman

Reputation: 4600

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

Upvotes: 1

malachay
malachay

Reputation: 76

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()

Upvotes: 6

Related Questions