Sunday, 13 January 2019

Groovy: Elvis operator (?:)


Elvis operator is a shortcut to ternary operator. It is used to assign some default value.

myOrganization = organization ? organization : "ABC Corp"

We can rewrite the above statement using elvis operator like below.

myOrganization = organization ?: "ABC Corp"

HelloWorld.groovy
organization = ""
myOrganization = organization ?: "ABC Corp"

println myOrganization

Output
ABC Corp

Previous                                                 Next                                                 Home

No comments:

Post a Comment