Java Ternary Operator

In Java, a ternary operator can be used to replace the if…else statement in certain situations. Before you learn about the ternary operator, make sure you visit Java if...else statement.


Ternary Operator in Java

A ternary operator evaluates the test condition and executes a block of code based on the result of the condition.

Its syntax is:

condition ? expression1 : expression2;

Here, condition is evaluated and

  • if condition is true, expression1 is executed.
  • And, if condition is false, expression2 is executed.

The ternary operator takes 3 operands (condition, expression1, and expression2). Hence, the name ternary operator.


Example: Java Ternary Operator

import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    
    // take input from users
    Scanner input = new Scanner(System.in);
    System.out.println("Enter your marks: ");
    double marks = input.nextDouble();

    // ternary operator checks if
    // marks is greater than 40
    String result = (marks > 40) ? "pass" : "fail";

    System.out.println("You " + result + " the exam.");
    input.close();
  }
}

Output 1

Enter your marks: 
75
You pass the exam.

Suppose the user enters 75. Then, the condition marks > 40 evaluates to true. Hence, the first expression "pass" is assigned to result.

Output 2

Enter your marks: 
24
You fail the exam.

Now, suppose the user enters 24. Then, the condition marks > 40 evaluates to false. Hence, the second expression "fail" is assigned to result.

Note: To learn about expression, visit Java Expressions.


When to use the Ternary Operator?

In Java, the ternary operator can be used to replace certain types of if...else statements. For example,

You can replace this code

class Main {
  public static void main(String[] args) {
    
    // create a variable 
    int number = 24;

    if(number > 0) {
      System.out.println("Positive Number");
    }
    else {
      System.out.println("Negative Number");
    }
  }
}

with

class Main {
  public static void main(String[] args) {
    
    // create a variable 
    int number = 24;

    String result = (number > 0) ? "Positive Number" : "Negative Number";
    System.out.println(result);
  }
}

Output

Positive Number

Here, both programs give the same output. However, the use of the ternary operator makes our code more readable and clean.

Note: You should only use the ternary operator if the resulting statement is short.


Nested Ternary Operators

It is also possible to use one ternary operator inside another ternary operator. It is called the nested ternary operator in Java.

Here's a program to find the largest of 3 numbers using the nested ternary operator.

class Main {
  public static void main(String[] args) {
    
    // create a variable
    int n1 = 2, n2 = 9, n3 = -11;

    // nested ternary operator
    // to find the largest number
    int largest = (n1 >= n2) ? ((n1 >= n3) ? n1 : n3) : ((n2 >= n3) ? n2 : n3);
    System.out.println("Largest Number: " + largest);
  }
}

Output

Largest Number: 9

In the above example, notice the use of the ternary operator,

(n1 >= n2) ? ((n1 >=n3) ? n1 : n3) : ((n2 >= n3) ? n2 : n3);

Here,

  • (n1 >= n2) - first test condition that checks if n1 is greater than n2
  • (n1 >= n3) - second test condition that is executed if the first condition is true
  • (n2 >= n3) - third test condition that is executed if the first condition is false

Note: It is not recommended to use nested ternary operators. This is because it makes our code more complex.

Before we wrap up, let’s put your knowledge of Java Ternary Operator (With Example) to the test! Can you solve the following challenge?

Challenge:

Write a function to check if a person can enter a club or not.

  • If age is greater than or equal to 18, return "Can Enter". Otherwise, return "Cannot Enter".
  • For example, if age = 21, the expected output is "Can Enter".
Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges