groovy - What means ?. in Java? -
this question has answer here:
- null-safe method invocation java7 3 answers
i've got such piece of code:
private string getusername(personalaccount account) { user usr = (user)account?.usr string name = usr?.getname() return name }
and in personalaccount class we've got field:
simpleuser usr
user extends simpleuser
what means this: ?. in 2 lines?
user usr = (user)account?.usr string name = usr?.getname()
that's not java, that's groovy. if java you'd have semicolons ending each statement.
the method returns name of user on account passed in, or null if account null or if user null.
it uses the safe-navigation operator. safe-navigation operator evaluates null if operand null, otherwise evaluates result of method call. way, if have method call on might null, don't have worry getting nullpointerexception.
Comments
Post a Comment