Java Ternary Operator

Filed Under: Java

Java ternary operator is the only conditional operator that takes three operands. Java ternary operator is a one liner replacement for if-then-else statement and used a lot in java programming. We can use ternary operator to replace switch also as shown in below example.

Java Ternary Operator

java ternary operator
The first operand in java ternary operator should be a boolean or a statement with boolean result. If the first operand is true then java ternary operator returns second operand else it returns third operand.

Syntax of java ternary operator is:

result = testStatement ? value1 : value2;

If testStatement is true then value1 is assigned to result variable else value2 is assigned to result variable.

Let’s see java ternary operator example in a simple java program.

package com.journaldev.util;
public class TernaryOperator {
    public static void main(String[] args) {
        String str = "Australia";
        String data = str.contains("A") ? "Str contains 'A'" : "Str doesn't contains 'A'";
        int i = 10;
        switch (i){
        case 5: 
        case 10:
        	System.out.println("i is not equal to 5 or 10");
        System.out.println((i==5) ? "i=5":((i==10) ? "i=10":"i is not equal to 5 or 10"));
    private static boolean invertBoolean(boolean b) {
        return b ? false:true;
    private static int getAbsoluteValue(int i) {
        return i<0 ? -i:i;
    private static int getMinValue(int i, int j) {
        return (i<j) ? i : j;

Output of the above ternary operator java program is:

Str contains 'A'

As you can see that we are using java ternary operator to avoid if-then-else and switch case statements. This way we are reducing the number of lines of code in java program. That’s all for a quick roundup of ternary operator in java.


  1. Adam says:

    System.out.println(“You guys reason well. That’s why i believe you are programmers.”);

  2. anil reddy says:

    Switch case is better than if else in performance wise. Ternary operator is better for simple conditions.

  3. sriram.c says:

    1. mostly use switch which is more efficient
    2. if you can’t use switch in some cases like non-matching stuff of strings use if… elseif…else
    3.use ternary rarely in easy & simple cases
    eg :
    if (a == true) {
    else {
    sop false
    a = (a = true) ? “true” : “false”;

    don’ t use in cases like :
    int age = 29;

    if (age < 13)
    System.out.println("You are but a wee child!");
    }// end if for age < 13
    else if (age < 19)
    System.out.println("You are no longer a child, but a budding teenager.");
    } // end else if for age < 19
    if (age < 65)
    System.out.println("You are an adult!");
    }// end if for age < 65
    System.out.println("You are now a senior, enjoy the good life friends!");
    } // end if for nested else
    System.out.println("Also, since you are over the age of 19, you deserve a drink!");
    }// end of final else

  4. Yashi says:

    This was really helpful?

  5. Abhi says:

    Hi Pankaj,

    Performance wise which one is better:
    1) if then …. else
    2) switch
    3) ternary operator

    1. uday kumar says:

      Hi Abhi,

      If we compare with if then else and switch statement.
      If you are having more if , else conditions it is better to go with Switch statement. Among the two performance wise switch is better than if else.

      I didn’t have any idea about ternary.


    2. sss says:

      ternary-operator is better

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors