java / beginner
Snippet
The Ternary Operator
The ternary operator is a shorthand for an if-else statement. It evaluates a condition and returns one of two values based on the result.
snippet.java
1
2
3
int age = 20;String status = (age >= 18) ? "Adult" : "Minor";System.out.println(status);
Breakdown
1
(age >= 18)
The boolean condition to be evaluated.
2
? "Adult"
The value returned if the condition is true.
3
: "Minor"
The value returned if the condition is false.