The ternary operator ?: is a one-line shorthand for a simple if-else. It evaluates a condition and returns one of two values.
int score = 85;
String result = (score >= 60) ? "Pass" : "Fail";
System.out.println(result); // Pass
The syntax is condition ? valueIfTrue : valueIfFalse. If the condition is true, the expression returns the value after ?. If false, it returns the value after :.
The ternary operator works best for simple choices. If your condition or values are complex, an if-else block is easier to read. Nesting ternaries inside other ternaries produces code that nobody wants to debug.