In this quick java tutorial, you will learn about the ternary operator.

Introduction

Ternary operator in Java is simple yet confusing. It’s quite often used in Java programming. And, you might come to an interviewer asking- “What is Java ternary operator?”

Ternary Operator in Java

Syntax of ternary operator-

condition ? expression1 : expression2;

Do you know that it is the only operator which takes 3 operants? Hence, the name ternary operator.

You can see the syntax is simple and easy to grab.

How does Java Ternary Operator work?

The working is similar to that of a conditional if-else statement. Its evaluation is simply a boolean value i.e either true or false depending on the condition(boolean expression).

You can better understand its working by going through a code example.

Example 1: Check if the number is Even or Odd


//Using if-else
public class EvenOdd {
    public static void main(String[] args) {

        int val = 5;

        if (val % 2 == 0) {
            System.out.println("Even number");
        } else {
            System.out.println("Odd number");
        }

    }
}

Likewise, the same result can be achieved by using the Ternary operator.


//Using Ternary Operator
public class EvenOdd {
    public static void main(String[] args) {

        int val = 5;

        String evenOdd = (val % 2 == 0) ? "Even number" : "Odd number";
        System.out.println(val + " is " + evenOdd);
    }
}

As you can see, we achieved the same result using the ternary operator(line number 9).

Output:

5 is Odd number

Note: Every if-else statement cannot be replaced with a ternary operator.

An advantage of using a ternary operator instead of using if-else is that –

  • The number of code lines required to achieve the same result is less.

Chained or Nested Ternary Operator

Yes, you can use a chained or nested ternary operator just like the nested if-else statement. Below, is a syntax-

condition ? expression1 : ( condition ? expression2 : expression 3);

Example 2: Java Program to check if the given number is positive, negative or equal to zero using ternary operator.


// nested ternary operator

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

        int num=0; //number to be checked

        // check if the number is positve, negative or equa to 0
        String msg = (num > 0 )? ("Number is positive integer" )
                     : ((num < 0 )? "Number is negative integer" 
                     : "Number is equal to 0");    

        System.out.println(msg);
    }
}

Output:

Number is equal to 0

A disadvantage of using the Ternary operator is that-

  • Ternary operator implementation becomes complex and reduces code readability for long case scenarios.

Thus, We avoid nested ternary operator for long case scenarios.

You can read more such Java tutorial- Enhanced for loop in Java

In conclusion, We hope this tutorial helped you understand – how the ternary operator in Java works? Keep learning keep sharing. Follow us on Facebook and Instagram.