Ternary operator
The ternary operator ? : is a shorthand for an if‑else statement that returns a value.
Syntax:
condition ? expressionIfTrue : expressionIfFalseExamples:
int age = 20;String status = (age >= 18) ? "Adult" : "Minor";System.out.println(status); // Adult
int max = (a > b) ? a : b; // assign the larger value
System.out.println((score >= 60) ? "Pass" : "Fail");Nested ternary (use sparingly for readability):
String grade = (score >= 90) ? "A" : (score >= 80) ? "B" : (score >= 70) ? "C" : "F";Important: Both expressions must be compatible types (or one must be assignable to the other). The result is the common type.
// int and double: result is doubledouble result = (x > 0) ? 10 : 2.5;