There can be various scenarios where it is required to specify the conditions in the code according to the requirements. In such situations, the “if-else” statements come into effect but the ternary operator makes this procedure more convenient by achieving the same in a single code line. Also, this functionality assists in nesting the conditions with minimal involvement of code, thereby streamlining it.
This guide will lead through the concept and working of the Ternary operator in Java.
What is the Ternary Operator in Java
The “Ternary Operator” is used in place of “if-else” conditions or even “switch” statements (in the form of nested ternary operators) that evaluates the provided test condition and runs a block of code based on the outcome of the condition.
Syntax
condition ? First Expression : Second Expression;
In this syntax, “condition” refers to the condition to be evaluated. It is such that if the condition is true, “First Expression” is invoked. Otherwise (the condition is false), the “Second Expression” comes into effect.
Graphical Representation of Ternary Operator

Example 1: Applying the Ternary Operator in Java
This example applies the ternary operator to evaluate the specified integer for the condition of the even or odd number:
public class TernaryOperator {
public static void main(String[] args) {
int value = 5;
String out = (value % 2 == 0) ? "Even Number" : "Odd Number";
System.out.println(out);
}}
In this code snippet:
- Initialize the value that needs to be evaluated.
- Now, apply the “ternary operator” to check if the specified number is even, i.e., completely divisible by “2”.
- Upon the met condition, the first expression will execute.
- Else, the second expression comes into effect.
Output

This output implies that the latter expression in the ternary operator is executed, returning the number as “odd”.
Example 2: Applying the Ternary Operator on the User Input Values
In this demonstration, the ternary operator can be applied to the user-entered values:
import java.util.Scanner;
public class TernaryOperator {
public static void main(String[] args) {
Scanner x = new Scanner(System.in);
System.out.println("Enter the age -> ");
int age = x.nextInt();
String result = (age > 18) ? "Eligible" : "Not Eligible";
System.out.println(result);
x.close();
}}
According to these code lines:
- Import the stated package to work with the “Scanner” class, enabling user input.
- Create a “Scanner” object and input the integer value from the user via the “nextInt()” method.
- Now, apply the “ternary operator” to invoke the corresponding expression based on the condition and close the Scanner.
Output

Here, it can be verified that both expressions are evaluated accordingly.
Example 3: Applying the Nested Ternary Operators in Java
In this demonstration, the nested ternary operators will be applied:
public class TernaryOperator {
public static void main(String[] args) {
int value1 = 3, value2 = 5, value3 = 7;
int result = (value1 < value2) ? ((value1 < value3) ? value1 : value3) :
((value2 < value3) ? value2 : value3);
System.out.println("Smallest Value -> " + result);
}}
According to these lines of code:
- Initialize the stated integers.
- After that, utilize the “nested ternary operators” to retrieve the smallest integer among the defined values.
- The nested ternary operators work such that if the condition becomes “true”, the former expression (comprising a condition and two expressions) will be invoked.
- In the scenario of the condition being “false”, the latter expression (containing a condition and two expressions) will be executed.
Algorithm
-> (3 < 5) ? ((1 < 7) ? 3 : 7) : ((5 < 7) ? 5 : 7);
-> ((1 < 7) ? 3 : 7) -> Invoked Expression.
-> result = 3
Output

This outcome signifies that the smallest specified value is retrieved based on the discussed algorithm.
Conclusion
A “ternary operator” in Java evaluates the provided test condition and runs a block of code based on the outcome of the condition. It can be used as a substitute for the “if-else” conditions or “switch” statements (in the form of nested ternary operators).