Home » Java Tutorial » Ternary Operator in Java with Examples

Ternary Operator in Java with Examples

We have seen the ternary operator in c, c++, and c#. Java also provides a ternary operator with the same working as c, c++, and c#. The ternary operator works the same as an if-else statement; the only difference is the number of code lines reduces. It can seem not very clear at first look, but the ternary operator can be used very effectively once mastered. 

When the code contains simple condition statements like checking even or odd, checking number positive or negative etc., with the if-else statement, this code would be of 6 to 7 lines. Using the ternary operator, we can replace those number of lines of code into a single line.  

How ternary operator works in Java?

The ternary operator takes three arguments to work. 

  1. Condition
  2. Expression 1
  3. Expression 2

Syntax

Condition ? Expresion 1 : Expresion 2;

If the condition’s output returns true, then the expression 1 satisfies, or the output of the condition returns false, then the expression two will be executed.

How to use ternary operator in Java?

To use the ternary operator, you need to understand the syntax of it. The ternary operator works with three arguments. First is the condition for which we are using the ternary operator. The second is expression 1. In expression 1, write that code that should be executed after the condition gets true. 

Check the examples

We can use a ternary in Java for many if-else statement programs like checking if two strings are equal or not, minimum or maximum of two numbers, etc. We can also use the ternary operator for nested conditions and the ternary operator in Java with multiple conditions. Let’s understand the examples one by one.

1. Checking if two strings are equal or not using the Ternary operator in Java.

Accept two strings from the user with a scanner class and pass it to the ternary operator with (==) condition. If both strings are equal, then the ternary operator will return if both strings are equal or if they are not equal, then it will return if both strings are not equal.

Example

import Java.util.Scanner;

public class ternary_operator {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter first string");
        String s1 =  sc.next();
        System.out.println("Enter second string");
        String s2 =  sc.next();
        String result = (s1 == s2) ? "Both strings are equal" : "Both strings are not equal";
        System.out.println(result);
    }
    
}

Output

Enter first string
Pawan
Enter second string
Pawan
Both are not same

2. The ternary operator in Java for checking a maximum of two numbers

Accept two numbers from the user using the scanner class. Pass those numbers to the ternary operator and print the returned value.

Example

import Java.util.Scanner;

public class ternary_operator {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter first number");
        int n1 =  sc.nextInt();
        System.out.println("Enter second number");
        int n2 =  sc.nextInt();
        String result = (n1>n2) ? "First number is greater than second" : "Second number is greater than first";
        System.out.println(result);
    }
    
}

Output

Enter first number
5
Enter second number
9
Second number is greater than first

3. The ternary operator in Java with multiple conditions

Just like the if-else ladder, we can pass multiple conditions to the ternary operator. It will return the appropriate output if the operator finds a correct match.

Example

import Java.util.Scanner;

public class ternary_operator {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter number of month");
        int month = sc.nextInt();
        String result = (month==1)?"January":(month==2)?"February": (month==3)?"March": (month==4)?"April": (month==5)?"May":(month==6)?"June": (month==7)?"July":(month==8)?"August":(month==9)?"September": (month==10)?"October": (month==11)?"November": (month==12)?"December":"Wrong number of month";
        System.out.println(result);
        
}
}

Output

Enter number of month
5
5th month is May

4. The nested ternary operator in Java.

Same as nested if-else statement, Java also provides the feature to write nesting conditions in ternary operator. This case will give appropriate output just reduces the number of lines of code.

Example

public class ternary_operator {
    public static void main(String[] args) {
    int a=10, b=30, c=20;
    int result = (a >= b) ? ((a >= c) ? a : c) : ((b >= c) ? b : c);
    System.out.println("Largest Number is " + result);
  }
}

Output

Largest Number: 30

How to write ternary operator in Java?

Syntax:

Data_type variable = Condition ? Expression_1 : Expression_2;

Example

String str = (n1<n2) ? "Number 1 is smaller than number 2" : "Number 2 is smaller than number 1";
Do you know?
1. Final Keyword in Java
2. Palindrome in Java
3. Wrapper class in Java
4. This Keyword in Java
5. Bubble Sort in Java
6. Armstrong Number in Java
7. Method overloading in java
8. Prime Number Program in Java
9. Type Casting In Java
10. Ternary Operator in Java

Pin It on Pinterest