Ternary Operator in Java

The ternary operator is a conditional operator and can be used as a replacement for using a simple if-else statement. In some cases, we can use ternary operator to replace even the switch statements.

In this article, we will learn about the ternary operator with examples; additionally, we will explore the concept of the nested ternary operator.

1. Ternary Operator

The core logic or algorithm behind the ternary operator is the same as if-else statement, only with less number of lines.

1.1. Syntax

The syntax for the ternary operator (? :) is given below. It takes three operands.

value = condition ? trueExpression : falseExpression;

The condition is a boolean valued expression that is either true or false.

  • If condition is true then the trueExpression will be executed;
  • otherwise, the falseExpression will be executed.

The return type of both true and false expressions will be of a similar type which will be assigned to a variable as per the result.

1.2. Example

In the following example, we are writing a statement using the if-else statement. And then we will rewrite the same statement using the ternary operator. This way, we will be able to understand how the ternary operator helps us in writing the simplified code expressions.

The below example uses the if-else statement. It is comparing a given number if the number is greater than 10 or not. In both cases, it will print a configured message.

int num  =  5;

If(num > 10) {
	System.out.println(“Number is greater than 10”); 
}  else  {
	System.out.println(“Number is smaller than 10”);
}

The above program prints: “Number is smaller than 10”.

Let’s rewrite the above code in a single line with the help of a ternary operator:

int num = 5;

String msg = num > 10  ?  "Number is greater than 10” :  "Number is smaller than 10”;

System.out.println(msg);

This code looks a lot cleaner and easy to understand.

2. Nesting Ternary Operator

It is possible to nest the ternary operator to any number of levels of our choice. In the nested ternary statement, the true and false expressions are other ternary statements.

In the following example, we are checking the largest of three integers. First, it checks the expression (i > j). If it returns true the expression (i > k ? i : k) gets executed, else the expression (j > k ? j : k) gets executed.

int i, j, k;

int value = (i > j) ? (i > k ? i : k) : (j > k ? j : k);

3. Conclusion

In this article, we learned about the ternary operator in Java with a few examples. Please note that it is not always possible to replace an if-else statement with a ternary operator, however, it is an awesome tool for some cases and makes our code shorter and more readable.

Happy Learning !!!

Was this post helpful?

Join 8000+ Awesome Developers, Like YOU!

Leave a Comment

About HowToDoInJava

This blog provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.