Table of contents
1.
Introduction
2.
What is the Ternary Operator in Java?
2.1.
Syntax of Ternary Operator in Java
3.
Flowchart of Ternary Operation
3.1.
Expressions of the Flowchart
4.
Examples of Ternary Operators in Java
4.1.
Example 1:
4.2.
Example 2:
4.3.
Example 3:
5.
When to Use the Ternary Operator?
6.
What is a Nested Ternary Operator?
7.
Advantages of Java Ternary Operator:
8.
Frequently Asked Questions
8.1.
Can a ternary operator have multiple statements in Java?
8.2.
What is the ternary operator for 3 conditions?
8.3.
What are the three operands in ternary operator?
8.4.
What is the difference between if-else and ternary operator in Java?
8.5.
Is ternary operator faster than if-else?
8.6.
What types of expressions can the ternary operator handle?
8.7.
What are the limitations to the ternary operator in Java?
9.
Conclusion
Last Updated: Nov 13, 2025
Medium

Ternary Operator in Java - Explanation with Examples

Author APURV RATHORE
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The Java ternary operator is a concise conditional operator that evaluates a boolean expression and returns one of two values based on the result. It uses the syntax condition ? valueIfTrue : valueIfFalse and is commonly used as a shorthand for simple if-else statements to make code more compact and readable.

Ternary Operator in Java

What is the Ternary Operator in Java?

Ternary Operators in Java is the only conditional operator in Java that takes three operands. It's a one-liner alternative to the if-then-else statement that's commonly used in Java programming. We may use the ternary operator instead of if-else conditions, or we can use nested ternary operators to swap conditions. Although the conditional operator follows the same algorithm as the if-else statement, it takes up less space and aids in the shortest possible writing of if-else statements.

Now, we are going to discuss the types of ternary operators. 

 

                                                                        

 

Syntax of Ternary Operator in Java

var= condition ? expression1 : expression2

 

The if-else statement corresponding to it will be.

if (condition){
var = expression1;
}
else{
var = expression2;
}

Flowchart of Ternary Operation

Flowchart of Ternary Operation

The ternary operator evaluates a condition and chooses between two expressions based on whether the condition is true or false. The flowchart typically has these steps:

  1. Start
  2. Evaluate Condition (e.g., condition)
  3. If condition is true: Execute Expression 1 (valueIfTrue)
  4. If condition is false: Execute Expression 2 (valueIfFalse)
  5. Return the result
  6. End

Expressions of the Flowchart

  • Condition: The boolean expression to evaluate (e.g., x > y)
  • Expression 1 (True path): Value or expression returned if the condition is true (e.g., x)
  • Expression 2 (False path): Value or expression returned if the condition is false (e.g., y)

In code, it looks like:

result = (condition) ? expression1 : expression2;

Examples of Ternary Operators in Java

Example 1:

Let the marks of a student be indicated by studentMarks. The boolean pass indicates that the marks of student is greater than 50 or not. 

studentMarks = 51;
pass = (studentMarks>50) ? true:false;

As studentMarks is greater than 50, the pass will be true.


The corresponding if-else statement will be. 

if (studentMarks>50){
pass = true;
}
else{
pass = false;
}

 

class A {
    public static void main (String[] args) {
        int studentMarks = 50;
        boolean pass = (studentMarks>50) ? true:false;
        System.out.println("Student passed : "+ pass);
    }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

Student passed : true

Example 2:

Suppose we want to calculate the absolute difference between two numbers num1 - num2. 
(num2>num1)? (num2-num1): (num1-num2);

The corresponding if-else statement will be. 

if (num1>num2){
result=num1-num2;
}
else{
result=num2-num1;
}

 

class A {
    public static void main (String[] args) {
        int num1 = 14;
        int num2 = 10;
        int result = (num2>num1)? (num2-num1): (num1-num2);
        System.out.println("Student passed : "+ result);
    }
}
You can also try this code with Online Java Compiler
Run Code


Output:

Result : 4

Example 3:

Let’s consider a situation where we need to check whether an integer number is even or odd. The ternary operator can be used to determine if a number is divisible by 2.

int number = 7;
String result = (number % 2 == 0) ? "Even" : "Odd";
System.out.println("The number is: " + result);

 

The corresponding if-else statement would be:

if (number % 2 == 0) {
    result = "Even";
} else {
    result = "Odd";
}

 

Output:

The number is: Odd

When to Use the Ternary Operator?

The ternary operator is a concise way to replace simple if-else conditions. It is useful when you need to assign a value based on a condition, making the code more compact and readable. It can be used for simple conditional assignments and does not require multiple lines of code as in the case of if-else.

Example:

int age = 18;
String status = (age >= 18) ? "Adult" : "Minor";
System.out.println("The person is an: " + status);

Output:

The person is an: Adult

 

Explanation:

  • If age >= 18, the ternary operator returns the value "Adult", otherwise, it returns "Minor".
  • This eliminates the need for an if-else block, making the code shorter and easier to read.

What is a Nested Ternary Operator?

A nested ternary operator is a ternary operator used inside another ternary operator. This allows you to check multiple conditions in a compact form.

Example for Nested Ternary Operators:

int score = 85;
String result = (score >= 90) ? "A+" : (score >= 80) ? "A" : (score >= 70) ? "B" : "C";
System.out.println("The grade is: " + result);

Output:

The grade is: A

 

Explanation:

  • First, it checks if the score is greater than or equal to 90. If true, it returns "A+".
  • If false, it moves to the next condition score >= 80, and so on, evaluating each condition until a valid result is found.

Advantages of Java Ternary Operator:

  • Concise Code: It allows you to write less code compared to traditional if-else statements, making the code more compact and readable.
  • Improved Readability: The ternary operator improves readability for simple conditions by providing a one-liner solution for conditional checks.
  • Reduced Lines of Code: Using a ternary operator reduces the need for multi-line if-else statements, which simplifies the code and reduces its length.
  • Faster Execution: The ternary operator can sometimes be more efficient than if-else as it directly evaluates the condition and assigns the result in one step.
  • Avoiding Extra Temporary Variables: You can directly assign values without needing additional temporary variables in the code, as it combines the condition check and the assignment in one line.
  • Better for Simple Conditions: It is ideal for simple boolean expressions. For complex conditions, traditional if-else may be more readable, but for simple ones, the ternary operator is a good choice.

Frequently Asked Questions

Can a ternary operator have multiple statements in Java?

No, the ternary operator in Java evaluates and returns a single expression, not multiple statements; use blocks or methods for multiple actions.

What is the ternary operator for 3 conditions?

For three conditions, nested ternary operators are used, chaining condition ? expr1 : (condition2 ? expr2 : expr3) to handle multiple cases.

What are the three operands in ternary operator?

The ternary operator uses three operands: a condition (boolean expression), a value if true, and a value if false.

What is the difference between if-else and ternary operator in Java?

if-else handles multiple statements with more readability; the ternary operator is a concise expression for simple conditional assignments or returns.

Is ternary operator faster than if-else?

The ternary operator is generally not faster than if-else in terms of performance. However, it can be more concise and easier to read, especially for simple conditions, but the execution time difference is negligible in most cases.

What types of expressions can the ternary operator handle?

The ternary operator can handle any expression that results in a value, such as boolean conditions, numerical comparisons, or object assignments. It is commonly used for conditional assignments or determining values based on simple conditions.

What are the limitations to the ternary operator in Java?

The ternary operator is limited in that it cannot handle complex logic or multiple statements. It is best suited for simple conditional assignments. For complex conditions, traditional if-else statements are recommended.

Conclusion

In this article, we have extensively discussed ternary operators in Java. It is a powerful tool for simplifying conditional expressions, making code more concise and readable. It allows for quick decisions in a single line, improving efficiency for simple conditions. While it is not suitable for complex logic, it is ideal for situations where a conditional assignment is needed.

Recommended Articles:

Live masterclass