Assign value if not exist in Groovy

Find this useful? Support us: Star on GitHub 6
Category: Other | Language: Groovy

In Groovy, there are multiple ways to assign a value if it does not exist. One of the most common ways is to use the ?: operator, also known as the Elvis operator. This operator returns the value on the left-hand side if it is not null, otherwise, it returns the value on the right-hand side. Here is an example:

def name = null
def defaultName = "John"

// Assign defaultName since name is null
name = name ?: defaultName

println name // Output: John

In the above example, the name variable is assigned the value of defaultName since it is initially null.

Another way to assign a value if it does not exist is to use the ?. operator, also known as the Safe Navigation operator. This operator allows you to safely navigate through a chain of properties or method calls, returning null if any intermediate object in the chain is null. Here is an example:

def person = ["name":"John"]
def age = person.age?.toString() ?: "Unknown"

println age // Output: Unknown

In the above example, the age variable is assigned the value of "Unknown" since the person.age property does not exist. The ?. operator prevents a NullPointerException from being thrown if the person object is null or its age property is null.

Both of these operators can be useful for assigning default values or handling null values in Groovy.