---
title: "Operators in Groovy"
canonical: "https://support.appfire.com/space/JMWE/461439869/Operators%20in%20Groovy"
format: markdown
---
> Macro (aura-html)

<span style="color: #404040">Groovy supports all typical operators of Java, such as the Unary operators, Arithmetic operators, Assignment operators, Logical operators, Relational operators and Conditional operator. </span><span style="color: #404040">See below for examples using some of the operators. </span><span style="color: #404040">For more information, see any </span><span style="color: #404040">[Java ](https://www.javatpoint.com/operators-in-java)</span><span style="color: #404040">[documentation](https://www.javatpoint.com/operators-in-java)</span><span style="color: #000000">.</span>

<span style="color: #404040">Apart from these, Groovy also features operators specific to the Groovy language such as the Safe navigation operator and the Elvis operator. </span>

<span style="color: #404040">**On this page:**</span>


## <span style="color: #404040">Typical operators</span>

##### **Unary operators**

```groovy
int x = 10

x++ //returns 11
++x //returns 12
x-- //returns 11
--x //returns 10
```

##### **Arithmetic operators**

```groovy
1 + 2 //returns 3
3 / 2 //returns 1.5
10 % 3 //returns 1

```

##### **Assignment arithmetic operators**

```groovy
def a = 5 
b = a += 3 //returns 8

def c = 5
c *= 3
c == 15

```

##### **Range operator**

```groovy
def range = 5..9
range[3] == 8
```

##### **Relational operators**

```groovy
a > b
b - 3 == a
b + 3 != a

```

##### **Logical operators**

```groovy
x && y
x || y
!x
```

##### **Conditional operators**

```groovy
!true == false
!'' == true
```

> ℹ️ **Ternary operator**
> ℹ️ 
> ℹ️ <span style="color: #222222">The ternary operator is a shortcut expression that is equivalent to an if/else branch assigning some value to a variable.</span>
> ℹ️ 
> ℹ️ <span style="color: #222222">Instead of:</span>
> ℹ️ 
> ℹ️ ```groovy
> ℹ️ def text = "Test"
> ℹ️ if (text!=null && text.length()>0) {
> ℹ️     result = text
> ℹ️ } else {
> ℹ️     result = 'Empty'
> ℹ️ }
> ℹ️ ```
> ℹ️ 
> ℹ️ you can simplify it to:
> ℹ️ 
> ℹ️ ##### **Ternary operator**
> ℹ️ 
> ℹ️ ```groovy
> ℹ️ def text = "Test"
> ℹ️ text ? text : 'Empty'
> ℹ️ ```

## <span style="color: #404040">Operators specific to Groovy</span>

<span style="color: #404040">There are notable operators that are specific only to the Groovy language; like the Elvis operator and the Safe Navigation Operator</span>

### <span style="color: #404040">Elvis operator</span>

<span style="color: #404040">Elvis operator is a </span><span style="color: #222222">shortening of the ternary operator. You need not have to </span><span style="color: #222222">repeat the value you want to assign. You can simplify the above example to:</span>

##### **Elvis operator**

```groovy
def text = "Test"
text ?: 'Empty'
```

### <span style="color: #404040">Safe navigation operator</span>

<span style="color: #222222">The Safe Navigation operator is used to avoid a NullPointerException. </span><span style="color: #222222">When you have a reference to an object you might need to verify that it is not</span><span style="color: #222222"> </span>`null`<span style="color: #222222"> </span><span style="color: #222222">before accessing methods or properties of the object. Using this operator, you can avoid this and directly return a </span>`null`<span style="color: #222222">.</span>

<span style="color: #222222">Normally you would have to:</span>

```groovy
if(employee){
  employee.getSalary()
}
```

Instead, you can write it as:

```groovy
employee?.getSalary()
```