The ternary operator in Java is the only operator that takes three operands. It has the form condition ? valueIfTrue : valueIfFalse: Java evaluates the condition, and then evaluates and returns exactly one of the two branches. It is a compact, expression-level replacement for a simple if-else that produces a value.
If you are searching for how the ternary operator java syntax behaves with types, nesting and null values, the sections below cover each case with small complete programs. You should already know about variables, boolean expressions and relational operators.
The general form is:
The condition must be a boolean expression. If it is true, the whole ternary expression has the value of expression1; otherwise it has the value of expression2. Only the chosen branch is evaluated, so the other branch never runs.
Here a > b is false, so the value after : is chosen. The value after ? is used when the condition is true, and the value after : when it is false.
The ternary operator is an expression, which means it produces a value. An if-else is a statement, which means it performs an action. That difference decides which one to use.
| Feature | Ternary operator | if-else statement |
|---|---|---|
| Kind | Expression, produces a value | Statement, controls flow |
| Can be used inside another expression | Yes | No |
| Can run several statements per branch | No, one expression each | Yes, any number |
Can initialise a final variable in one line | Yes | Only with careful definite assignment |
| Best for | Choosing between two simple values | Branches with side effects or several steps |
Use the ternary when both branches are short values. Use if-else when a branch needs several statements, logging, or early returns.
Because the ternary produces a value, you can use it directly as a method argument, in a string, or in a return statement.
Notice the parentheses around the ternary inside the string concatenation. The + operator has higher precedence than ?:, so without the parentheses Java would read the whole concatenation as the condition and fail to compile.
A branch can itself be another ternary expression. The operator is right-associative, so a ? b : c ? d : e means a ? b : (c ? d : e).
Laid out one condition per line like this, a short chain is readable. Beyond two or three levels, switch to an if-else if chain or a switch expression, which is easier to read and to change.
Both branches should have compatible types, because the whole expression has one type. Java applies promotion rules when they differ.
Three things to remember:
int and the other double, the result is double, so 1 prints as 1.0.char and the other an int constant that fits in a char, the result is a char. If the int is a variable, the result is promoted to int and prints a number.null, Java unboxes the wrapper and throws NullPointerException when it is null. Make both branches the same wrapper type or check for null first.flag ? doA() : doB(); as a statement. This does not compile, because a ternary is not a valid expression statement. Use if-else instead.count ? "yes" : "no" with an int. Java has no truthy values, so write count != 0.+ or other operators.No. It only chooses between two expressions that produce values. Branches that need several statements, loops or return in the middle still need if-else.
No meaningful difference. The compiler produces very similar bytecode, so choose based on readability, not speed.
Not as a standalone statement. You must use the value, for example by assigning it, returning it or passing it to a method.
It returns that value regardless of the condition, but the condition is still evaluated. This is rarely useful and usually points to a mistake.
The ternary operator condition ? a : b is Java's compact way to pick one of two values. It is an expression, so it can sit inside assignments, returns and arguments. Keep branches short and of compatible types, wrap it in parentheses when combined with other operators, avoid unboxing null, and fall back to if-else or switch when the logic grows.