Java Ternary Operator: One-line Condition

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 the ternary operator to replace even the switch statements. Instead of writing lengthy if-else constructs for basic conditions, we can use the ternary operator to achieve the same result in a more concise manner.

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

1. What is the Ternary Operator?

The ternary operator is an operator which evaluates a condition and chooses one of two cases to execute. It is also called the conditional 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 the 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. 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 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”;


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.

However, nesting ternary operators should be done with caution as it can reduce readability.

Happy Learning !!!


Notify of
Inline Feedbacks
View all comments

About Us

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

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

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode