Java Ternary Operator

Last Updated : 20 Dec, 2025

The ternary operator is a compact alternative to the if-else statement. It evaluates a condition and returns one of two values depending on whether the condition is true or false. It is commonly used for:

  • Conditional value assignment
  • Simple decision-making logic
  • Replacing short if-else blocks

Note: Ternary operators improve readability for simple conditions. For complex logic, if-else statements are preferred.

Example:

Java
import java.io.*;

class GFG {
    public static void main (String[] args) {
        int a = 10, b = 20;
        int res = (a > b) ? a : b;
        System.out.println(res);
    }
}

Output
20

Explanation: If a > b evaluates to true, a is assigned to res, otherwise, b is assigned to res.

conditional_or_ternary_operator_in_java
Ternary Operator Syntax

Syntax

variable = Expression1 ? Expression2: Expression3

Explanation:

  • If Expression1 is true, Expression2 is executed
  • If Expression1 is false, Expression3 is executed.
  • The result is assigned to variable.

Equivalent if-else Statement

if (Expression1) {
variable = Expression2;
} else {
variable = Expression3;
}

Flowchart of Ternary Operation  

The diagram below demonstrates the flow chart of ternary operation:

flow_chart_of_conditional_or_ternary_operator
Flow chart of cnoditional statement

Explanation:

  • Expression1 is evaluated.
  • If true, Expression2 is executed.
  • If false, Expression3 is executed.
  • The evaluated value is assigned to the variable.

Example 1: Find the Largest of Two Numbers

The code depicts how the ternary operator can be used to compare two values and assign the larger one to a variable in a single line.

Java
class Geeks {
    public static void main(String[] args) {

        int n1 = 5, n2 = 10;
        int max = (n1 > n2) ? n1 : n2;
        System.out.println("Maximum is = " + max);
    }
}

Output
Maximum is = 10

Explanation:If n1 > n2 evaluates to true, n1 is assigned to max; otherwise, n2 is assigned.

Example 2. Perform Arithmetic Operation

To choose between two arithmetic operations based on a condition, ternary operator is used.

Java
class Geeks {
    public static void main(String[] args) {

        int n1 = 5, n2 = 10;
        int result = (n1 > n2) ? (n1 + n2) : (n1 - n2);
        System.out.println("Result = " + result);
    }
}

Output
Result = -5

Explanation: When n1 > n2 is false, the subtraction expression is executed and its result is stored in result.

Example 3: Assign Value Based on Boolean Expression

Ternary operator assigns a value based on a boolean expression.

Java
class Geeks {
    public static void main(String[] args) {

        boolean flag = true;
        String result = flag ? "True" : "False";
        System.out.println(result);
    }
}

Output
True

Explanation: If flag is true, the string "True" is assigned to result; otherwise, "False" is assigned.

Advantages of Java Ternary Operator

  • Concise syntax for simple conditional logic
  • Reduces code length compared to if-else
  • Improves readability when used correctly
  • Efficient execution for single-expression evaluation
  • Ideal for conditional assignments

Limitations of Ternary Operator

  • Reduces readability when nested
  • Not suitable for complex logic
  • Difficult to debug when overused
Comment
Article Tags: