The ternary operator in Java is an operator that takes in three values, or operands. It is a compact form of an if-else statement, that returns a value based on the condition provided.

Syntax of the ternary operator in Java

As mentioned earlier, the ternary operator takes in three operands. The first operand is a boolean expression, and the other two are expressions of the same data type. The ternary operator is written like this:

booleanExpression ? expression1 : expression2

If the first operand (booleanExpression) evaluates to true, it returns the second operand (expression1), otherwise it returns the the third operand (expression2). Since the ternary operator returns a value, we can assign the returned value to a variable, as we will see in the next section.

Examples of the ternary operator in Java

Suppose, we are given two variables, and we want to assign the greater value among them to a new variable max. Here's how we'd do it using the ternary operator:

int a = 20, b = 10;
int max = a > b ? a : b;
System.out.println(max); // prints 20

First, the boolean condition a > b is evaluated. Since 20 is greater than 10, the condition evaluates to true, and the second operand a is returned. Thus, the max variable is set to 20. On the other hand, if we modified the value of a and b like this:

int a = 10, b = 15;
int max = a > b ? a : b;
System.out.println(max); // prints 15

In this case, a > b evaluates to false, which means the third operand b is returned, and so the max variable is set to 15.

The ternary operator can be used for other data types as well. Suppose we have two variables and we want to display a message stating which variable is greater. We can do it like so:

int a = 10, b = 15;
System.out.println(a > b ? "a is greater" : "b is greater"); // prints b is greater

In this case, a > b evaluates to false, so the string "b is greater" is passed to System.out.println(), and the same message is printed to the console.

Evaluation order for the ternary operator

As discussed before, the ternary operator takes the following form:

booleanExpression ? expression1 : expression2

While booleanExpression is always evaluated, expression1 or expression2 is only evaluated based on the result of booleanExpression. Let us try to understand this with an example.

Consider the following program, where we check if a > b using the ternary operator, and based on the result, we call foo() or bar() and assign the returned value to result.

static int foo() {
    System.out.println("called foo");
    return 100;
}

static int bar() {
    System.out.println("called bar");
    return 200;
}

public static void main(String[] args) {
    int a = 7, b = 3;
    int result = a > b ? foo() : bar();
    System.out.println(result);
}

In this case, a > b evaluates to true, which means only the method foo() is called. Once foo() returns 100, it is assigned to the result variable. We can confirm the fact that only foo() is called by checking the output:

called foo
100

Had we changed the boolean expression of the ternary operator to a < b, then only the bar() method would have been called, as confirmed by the output:

called bar
200

Therefore, only one of the two right hand expressions of the ternary operator are evaluated at any given time.

Nesting the ternary operator

Since the ternary operator returns a value, it can be combined, or "nested" with another ternary operator.

Suppose, we have a integer variable a and we want to print the following message based on its value:

  • If a < 5, we'll print "The value is too small".
  • If 5 <= a <= 10, we'll print "The value is between 5 and 10"
  • Otherwise, we'll print "The value is too large"

Since there are two different conditions to check for, we can nest two instances of the ternary operator to do so, as shown below.

int a = 4;
String message = a < 5 ? "The value is too small" : a > 10 ? "The value is too large" : "The value is between 5 and 10";
System.out.println(message); // prints The value is too small

There are two ternary operators at work here:

Nested ternary operator

In the above example, a = 4, so the condition of the outer ternary operator, a < 5 evaluates to true, and the string "The value is too small" is returned.

However, if we had a = 6, the condition of the outer ternary operator, a < 5 evaluates to true, and the inner ternary operator is evaluated next. The condition of the inner ternary operator, a > 10 evaluates to false, hence, the string "The value is between 5 and 10" is returned.

It can be cumbersome to nest ternary operators, so for more complex use cases you should use an if-then-else statements, or alternatively you can use brackets to clearly seperate the operators, like so:

a < 5 ? "The value is too small" : (a > 10 ? "The value is too large" : "The value is between 5 and 10")