Back to Operators & Expressions
Q5Question 5 of 9

What is the ternary conditional operator?

Key Concepts
  • Syntax: condition ? valueIfTrue : valueIfFalse – returns one of two values based on a boolean
  • It is an expression (returns a value), not a statement like if-else
  • Both branches must produce compatible types – the result type follows promotion rules
  • If one branch is Integer (wrapper) and the other is int (primitive), both are unboxed – null wrapper causes NPE
  • Use for simple, one-line value selection; avoid for complex logic – readability matters
  • Can be nested but deeply nested ternaries are hard to read – prefer if-else or switch

Definition

The ternary operator (condition ? a : b) returns a if the condition is true, b if false. It is a compact alternative to if-else for simple value selection.

The ternary operator has three parts:

  • Condition: A boolean expression evaluated first.
  • True branch: The value returned if the condition is true.
  • False branch: The value returned if the condition is false.

It is the only operator in Java that takes three operands. It returns a value, which makes it an expression you can use inside assignments, method arguments, and return statements.

A loan officer checks eligibility: "Is the credit score above 700? If yes, the rate is 8.5%. If no, the rate is 12%." The ternary operator is this one-line decision: rate = score > 700 ? 8.5 : 12.0.

Understanding the Concepts

How does the ternary operator work?

JAVA
int creditScore = 720;
double rate = creditScore >= 700 ? 8.5 : 12.0;
System.out.println("Rate: " + rate);  // Rate: 8.5

String status = creditScore >= 700 ? "APPROVED" : "REVIEW";
System.out.println("Status: " + status);  // Status: APPROVED

This is equivalent to:

JAVA
double rate;
if (creditScore >= 700) {
    rate = 8.5;
} else {
    rate = 12.0;
}

Where can you use it?

Because the ternary is an expression (it produces a value), you can use it in places where if-else cannot go:

JAVA
// In an assignment
String label = amount > 0 ? "CREDIT" : "DEBIT";

// In a method argument
log.info("Type: " + (isActive ? "ACTIVE" : "CLOSED"));

// In a return statement
return balance.compareTo(BigDecimal.ZERO) >= 0 ? "Positive" : "Overdrawn";

// Inside string concatenation
System.out.println("Account is " + (active ? "active" : "inactive"));

What type does the ternary return?

The result type follows numeric promotion rules if the two branches have different types:

JAVA
// Both int: result is int
int fee = premium ? 0 : 50;

// int and double: result promoted to double
double rate = eligible ? 8 : 12.5;  // 8 promoted to 8.0

// String and String: result is String
String type = savings ? "SAVINGS" : "CURRENT";

What is the null unboxing trap?

If one branch is a wrapper and the other is a primitive, both are unboxed. A null wrapper causes NPE:

JAVA
Integer fromDb = null;  // nullable database column

// TRAP: both branches unboxed to int, fromDb is null → NPE!
// int value = true ? fromDb : 0;

// FIX: null-check before the ternary
int value = fromDb != null ? fromDb : 0;

Can you nest ternaries?

JAVA
// Nested: works but hard to read
String tier = score >= 750 ? "GOLD" : score >= 600 ? "SILVER" : "BRONZE";

// Better: use if-else or switch for 3+ branches
String tier;
if (score >= 750) {
    tier = "GOLD";
} else if (score >= 600) {
    tier = "SILVER";
} else {
    tier = "BRONZE";
}

Nested ternaries are legal but reduce readability. Use them only for 2-way decisions.

What are common mistakes?

DIAGRAM
- **Missing parentheses in concatenation.** `"Value: " + flag ? "yes" : "no"` is parsed as `("Value: " + flag) ? ...` which fails. Write `"Value: " + (flag ? "yes" : "no")`.
- **Null wrapper unboxing.** `condition ? nullInteger : 0` unboxes both branches → NPE if the null branch is selected.
- **Complex logic in branches.** Ternary with method calls, calculations, or side effects is hard to debug. Use if-else.

What should a senior engineer know?

DIAGRAM
- **Ternary is an expression, if-else is a statement.** You can assign a ternary to a final variable, which is cleaner than declaring a variable and assigning in both branches of an if-else.
- **Switch expression (Java 14+) replaces nested ternaries.** `var tier = switch(score) { case s when s >= 750 -> "GOLD"; ... };`
- **In lambdas and streams,** ternary is often the only option for inline branching: `list.stream().map(a -> a.isActive() ? "Y" : "N")`.

Visual Overview

DIAGRAM
  condition ? valueIfTrue : valueIfFalse
       │
       ├─ condition is true  → return valueIfTrue
       └─ condition is false → return valueIfFalse

  Type rules:
    both same type → that type
    int and double → double (promotion)
    Integer and int → int (unboxing – null Integer → NPE!)

Key Takeaway

The ternary operator is a compact expression that returns one of two values based on a boolean condition. It is ideal for simple, one-line value selection where if-else would be verbose. Use it for assignments, return values, and inline expressions.

Watch out for the null unboxing trap: if one branch is a wrapper and the other is a primitive, both are unboxed, and a null wrapper causes NullPointerException. And avoid nesting ternaries deeply – if you have three or more branches, use if-else or a switch expression.

One sentence to remember: condition ? a : b returns a if true, b if false – use it for simple two-way decisions, and always null-check wrapper branches.

---

Share
Use ← → arrow keys to navigate