Table of Contents
Introduction
In Java, operators are symbols or keywords that perform specific operations on variables or values. There are several types of operators in Java. One of them is Ternary Operator.
The ternary operator is a shorthand way of writing a conditional statement in Java. It is also known as the conditional operator or the ternary conditional operator.
Ternary Operator in Java
The ternary operator is often used as a shorthand way of writing simple if-else statements, particularly when assigning values to variables.
The syntax of the ternary operator is as follows:
<condition> ? <value if true> : <value if false>
Here, <condition> is a boolean expression that evaluates to either true or false, <value if true> is the value to be returned if the condition is true, and <value if false> is the value to be returned if the condition is false. Both these values are separated by a question mark (?) and a colon (:).
Here is a flowchart that shows the working of the Java ternary operator.

Let's look at the examples of using Ternary Operator in Java.
Example 1
Here is the Java Code:
package test1; public class ternaryOperatorJava { public static void main(String args[]){ int age = 21; String status = (age >= 18) ? "Adult" : "Minor"; System.out.println(status); } }
The Output will be -
Adult
Explanation:
In this example,
- The ternary operator is used to assign the value "Adult" to the variable status if the value of age is greater than or equal to 18, and
- The value "Minor" if the value of age is less than 18.
- Since the value in the age variable is 21, which is greater than 18, the ternary operator assigns the value "Adult" to the variable status.
Example 2
Here is the Java Code:
package test1; public class ternaryOperatorJava { public static void main(String args[]){ int a = 1; int b = 2; int c = (a > b) ? (a - b) : (a + b); System.out.println(c); } }
The Output will be -
3
Explanation:
In the above example code,
- First, two integer variables, a and b, are declared and initialized with the values 1 and 2, respectively.
- Then, a third integer variable, c, is declared and initialized with the result of a ternary operator.
- This ternary operator checks whether the value of a is greater than that of b. If it is, the expression (a - b) is evaluated, and the result is assigned to c. If not, the expression (a + b) is evaluated, and the result is assigned to c.
- Since 1 is not greater than 2, the expression (a + b) is evaluated, and the result, 3, is assigned to c.
- Finally, the value of c is printed to the console using the System.out.println() method.
Nested Ternary Operator
When one ternary operator is used inside another ternary operator, it is called a nested ternary operator in Java.
The nested ternary operator allows for more complex and concise conditional expressions and can be a helpful tool for reducing code complexity and improving readability.
In Java, the syntax of a simple nested ternary operator is as follows:
(condition1) ? value1 :
(condition2) ? value2 :
(condition3) ? value3 : value4
In this syntax,
- the first condition(condition1) is evaluated. If it's true, the first value(value1) is returned.
- If it's false, the second condition(condition2) is evaluated. If it's true, the second value(value2) is returned.
- If it's false, the third condition(condition3) is evaluated. If it's true, the third value(value3) is returned.
- If it's false, the fourth value(value4) is returned.
The syntax above was a demonstration of a simple nested ternary operator. However, we can nest multiple ternary operators inside each other to create more complex expressions.
Here are examples of using the nested ternary operator in Java:
Example 1
Here is the Java Code:
package test1; public class nestedTernaryOperatorJava { public static void main(String args[]){ int age = 20; String result = (age >= 18) ? ((age <= 30) ? "Young adult" : "Adult") : "Minor"; System.out.println(result); } }
The Output will be -
Young adult
Explanation:
In this example,
- The nested ternary operator is used to determine the value of the result variable based on the value of the age variable.
- If age is greater than or equal to 18, the nested ternary operator determines whether age is less than or equal to 30.
- If age is less than or equal to 30, the value "Young adult" is assigned to result.
- Otherwise, the value "Adult" is assigned to result.
- If age is less than 18, the value "Minor" is assigned to result.
Example 2
Here is the Java Code:
package test1; public class nestedTernaryOperatorJava { public static void main(String args[]){ int a = 1; int b = 2; int c = -3; int d = (a >= b) ? ((a >= c) ? a : c) : ((b >= c) ? b : c); System.out.println(d); } }
The Output will be -
2
Explanation:
In this example,
- First, three integer variables, a, b, and c, are declared and initialized with the values 1, 2, and -3, respectively.
- Then, a fourth integer variable, d, is declared and initialized with the result of a nested ternary operator. This nested ternary operator checks three conditions:
- If a is greater than or equal to b, then:
- If a is greater than or equal to c, then the value of a is assigned to d.
- Otherwise, the value of c is assigned to d.
- Otherwise (if a is less than b), then:
- If b is greater than or equal to c, then the value of b is assigned to d.
- Otherwise, the value of c is assigned to d.
- If a is greater than or equal to b, then:
- In this case, a is not greater than or equal to b, so the second condition is evaluated. Since b is greater than c, the value of b(2) is assigned to d.
- Finally, the value of d is printed to the console using the System.out.println() method.
Nested ternary operators can be a powerful tool for writing concise and readable code, but they should be used cautiously.
As with any conditional operator, nesting too many ternary operators can make the code difficult to read and understand and increase the risk of introducing bugs or errors into the code.
As a general rule, it's best to limit the use of nested ternary operators to cases where they improve code readability and maintainability and to break up complex expressions into smaller, more manageable chunks whenever possible.
Advantages of using the Ternary operator in Java
There are several advantages to using the ternary operator in Java:
- Concise syntax: The ternary operator provides a compact and concise way to write conditional statements in Java, which can help to improve code readability and reduce code clutter.
- Saves lines of code: Using the ternary operator can help reduce the lines needed to express a conditional statement, making the code easier to read and maintain.
- Avoids repetition: In some cases, using the ternary operator can help to avoid code repetition and duplication, as the same code block can be reused for both the true and false conditions.
- Faster performance: In some cases, using the ternary operator can lead to faster performance than if-else statements, as the ternary operator requires less overhead and has less Space complexity.
- Encourages good coding practices: Using the ternary operator can encourage good coding practices, such as using meaningful variable names and avoiding overly complex expressions.
However, it is essential to use the ternary operator judiciously and only in cases where it improves code readability and maintainability.
Drawbacks of using the Ternary operator in Java
While the ternary operator can be helpful in some cases, there are also some potential drawbacks to using it in Java:
- Reduced readability: In some cases, using the ternary operator can reduce code readability, mainly when the conditions and values are complex or difficult to understand. This can make the code harder to maintain and debug.
- Limited scope: The ternary operator is best suited for simple conditional statements that involve a single condition and two possible outcomes. Using if-else statements or switch statements may be more appropriate for more complex statements involving multiple conditions or more than two possible outcomes.
- Type conversion issues: The ternary operator can sometimes result in type conversion issues if the values for the true and false conditions have different data types. In some cases, this can lead to unexpected behavior or errors.
- Code duplication: In some cases, using the ternary operator can result in code duplication, particularly if the same code block is repeated for both the true and false conditions. This, too, can make the code harder to maintain and debug.
- Debugging difficulties: When using the ternary operator, setting breakpoints and stepping through code during debugging can be more challenging. The ternary operator is often treated as a single expression by the debugger. This can make it harder to identify and fix issues in the code.
Overusing the ternary operator can lead to overly complex code and reduce code readability, so balancing the advantages and potential drawbacks is essential.
I hope you found this article helpful.

You may like to Explore -
Integer compare() method – Java – Explained with examples
Convert a Char to String in Java
Modulus Operator (Modulo or Remainder) in Java
Cheers!
Happy Coding.
About the Author
This article was authored by Ishani Samanta. Verified by Rawnak.