Java Ternary Operator

Summary: in this tutorial, you will learn about the Java ternary operator and how to use it to make your code more concise.

Introduction to the Java ternary operator

The ternary operator, represented as ?: is a concise way to express if-else statements.

The ternary operator evaluates a condition and returns a value if the condition is true and another value if the condition is false.

The syntax of the Java ternary operator is as follows:

condition ? valueIfTrue : valueIfFalse;Code language: Java (java)

In this syntax:

  • condition is a boolean expression that can be true or false.
  • valueIfTrue is a return value if the condition is true.
  • valueIfFalse is a return value if the condition is false.

The operator ?: is called a ternary operator because it accepts three operands. The ternary operator is also known as a conditional operator.

Typically, you assign the return value of the ternary operator to a variable like this:

variable = condition ? valueIfTrue : valueIfFalse;Code language: Java (java)

It is equivalent to the following:

if (condition) {
    variable = valueIfTrue;
} else {
    variable = valueIfFalse;
}Code language: Java (java)

For example, the following program uses an if-else statement to find the maximum value of two values:

public class App {
    public static void main(String[] args) {
        int x = 10, y = 20;
        int max;
        if (x > y) {
            max = x;
        } else {
            max = y;
        }
        System.out.println(max);
    }

}Code language: Java (java)

Output:

20

To make the code more concise, you can use the ternary operator as follows:

public class App {
    public static void main(String[] args) {
        int x = 10, y = 20;
        int max = x > y ? x : y;
        System.out.println(max);
    }
}Code language: Java (java)

In this example, the whole if-else statement consists of 6 lines of code:

int max;
if (x > y) {
    max = x;
} else {
    max = y;
}Code language: Java (java)

was replaced by a single line of code that uses the ternary operator:

int max = x > y ? x : y;Code language: Java (java)

As you can see, the ternary operator does make your code more concise and expressive.

Nested ternary operator

Technically, Java allows you to nest a ternary operator. The valueIfTrue and/or valueIfFalse can be replaced with a nested ternary operator.

For example, the following replaces the valueIfTrue and valueIfFalse by two other nested ternary operators:

condition ? (condition2 ? value2IfTrue : value2IfFalse) 
          : (condition3 ? value3IfTrue : value3IfFalse);Code language: Java (java)

The following program illustrates how to use the nested ternary operators to find the maximum number of three numbers:

public class App {
    public static void main(String[] args) {
        int x = 10, y = 20, z = 30;
        int max = x > y ? (x > z ? x : z): (y > z ? y : z);
        System.out.println(max);
    }
}Code language: Java (java)

Output:

30

The program first compares x with y. If x is greater than y, it compares x with z. Otherwise, it compares y with z. Finally, it returns the maximum value between x, y, and z.

In practice, you should avoid using the nested ternary operators because they make your code difficult to understand.

Summary

  • Java ternary operator returns a value if a condition is true and another value if the condition is false.
  • Use the ternary operator to make your code more concise.
  • Avoid using nested ternary operators that make your code more difficult to understand. Readability counts.
Was this tutorial helpful ?