What is ternary operator in Java?

Ternary operator (?:) is a different, more concise version of the if statement.

The if statement just executes either one or the other block of code:

boolean someCondition = true;

if (someCondition) {
  // this will execute
} else {
  // this will not execute
}

The ternary operator executes either one or the other statement (not a block) and returns the result.

It's typically used to decide which value to use:

boolean someCondition = true;

int result = someCondition ? 33 : 44;

System.out.println(result); // 33