Ternary Operator in Salesforce Apex

Ternary Operator : (x ? y : z)

Ternary operator is a one liner replacement for if-then-else statement. If x, a Boolean, is true, y is the result. Otherwise z is the result. Note that x cannot be null.

Example:

Boolean isLeapYear = true;

//Using Ternary Operator
String msg = isLeapYear ? 'It is a leap year.' : 'It is not a leap year.';
System.debug('Message - ' + msg);

If “isLeapYear” is true, assign the message value to “It is a leap year”, else assign message value to “It is not a leap year”.