Conditional Operator
When the only operation being performed inside of an if
and else
pair
is setting the initial value of a variable, you can use the "conditional operator"1
to perform that assignment instead.
int age = 22;
String message = age < 25
? "You cannot rent a car!"
: "You might be able to rent a car";
You write a condition followed by a ?
, a value to use when that condition evaluates to true
, a :
,
and then a value to use when that condition evaluates to false
.
CONDITION ? WHEN_TRUE : WHEN_FALSE
1
Some people will call this a ternary expression. Ternary meaning "three things." Same idea as tres leches.