Java

Ternary/Conditional Operator in Java

In Java, ternary operators are used in place of if-else conditional statements. The ternary operator provides the same result as of if-else statement, but it is the shortest way of using the if-else statement. Moreover, a nested ternary operator may also be used to replace the switch case statement. This article provides the working and usage of the ternary operator in Java.

How the ternary operator works in Java

The working of the ternary operator depends on the following syntax:

(condition) ? expression1: expression2

As the name of the operator is ternary, it depends on three instances as shown in the syntax above. The symbol “?” is referred to as the ternary operator that combines condition, expression1, and expression2 in a single line.

Further description of the instances used in the syntax is provided below:

  • condition: A condition is defined
  • expression1: This part is executed if the condition is true
  • expression2: If the condition is false, expression2 will be executed.

How to use ternary operator in Java

This section demonstrates several examples that show the usage of the ternary operator in Java. For better understating, the usage of the ternary operator is categorized into the following:

Using the simple ternary operator

The following Java code practices the use of a ternary operator to check the variable is a digit or a number.

package lh;

public class TernaryOperator {
   
    public static void main(String[]args) {
   
        int a = 5;

        String n = (a >= 10) ? "number" : "digit";
        System.out.println("The variable is a: " +n );
    }
}

The code is described below:

  • creates a new variable a
  • applies a ternary operator to check that the variable is a number or a digit and then stores the result in a string variable n. The ternary operator checks the condition (a>=10), as it is false so the “digit” will be stored in the string variable n.
  • prints the string variable n

The image below shows the code and the output console:

Using Nested ternary operator

When multiple ternary operators (?) are used in a single line, we refer to them as the nested ternary operator. The nested ternary operator act as a replacement for switch-case statements. The following code demonstrates the use of nested ternary operator:

package lh;

public class TernaryOperator {
   
    public static void main(String[]args) {
       
        int a = 5, b=6, c=7;
       
        //applying nested ternary operator
        int n = (a <= b) ? ((a <= c) ? a : c) : ((b <= c) ? b : c);
       
        System.out.println("The smallest number is: " +n );
    }
}

The above-stated code is described as below:

  • declares three integer variables
  • applies a nested ternary operator to check for the smallest integer and stores the value in a new integer named n. Moreover, the figure provided below better demonstrates the nested ternary operator used in the above example.

  • the value of variable n is printed

The image of code and output is provided below:

Conclusion

The ternary operator is the best replacement for if-else and switch-case statements. They work on the same logic as if-else and switch statements work. Alternatively, the ternary operator performs these operations in an easy and effective way by using a single line expression. In this post, the syntax and usage of the ternary operator are discussed in detail. You would have learned to use a single as well as the nested ternary operator in Java. A simple ternary operator is associated with an if-else statement whereas the nested ternary can be used for switch-case statements.

About the author

Adnan Shabbir