Java Ternary Operator Example | Ternary Operator In Java Tutorial

Before you learn about the ternary operator, you need to understand the if…else statement in Java. Ternary operators can be used to replace the if…else statement. Java ternary operator is an only conditional operator that takes the three operands.

Java Ternary Operator Example

Syntax of java ternary operator is following.

output = expression ? value1 : value2;

If the expression is true, then value1 is assigned to the result variable else value2 is attached to the output variable. The condition is the Java expression that evaluates to either true or false. See the following code example.

public class Conditionals {
  public static void main(String[] args) {
    Double input = -21.19;
    String output;

    output = (input > 0.0) ? "positive" : "not positive";
    System.out.println(input + " is " + output);
  }
}

See the following program.

public class Conditionals {
  public static void main(String[] args) {
    Double input = -21.19;
    String output;

    output = (input > 0.0) ? "positive number" : "negative number";
    System.out.println(input + " is " + output);
  }
}

See the following output.

➜  java javac Conditionals.java
➜  java java Conditionals
-21.19 is negative number
➜  java

#Ternary Operator Condition

See the following code.

public class Conditionals {
  public static void main(String[] args) {
    Double input = -21.19;
    String output;

    output = (input > 0.0) ? "positive number" : "negative number";
    System.out.println(input + " is " + output);
  }
}

In the above code, our Ternary Operator condition is the following.

output = (input > 0.0) ?

So, before the question mark, the expression here is the condition.

So, if the input is greater than 0, then it is a positive number, otherwise a negative number.

#Ternary Operator Values

In the above section, we have seen the Ternary Operator condition, and now we will see ternary operator values. See the following syntax.

"positive number" : "negative number"

So, based on the condition, the ternary operator will give us either a positive number or a negative number. Here, a positive number and a negative number is the ternary operator.

#When to use the ternary operator

You can replace the multiple lines of code with the single line of code using the ternary operator. It makes your code more readable. However, don’t overdo it. For example,

You can replace the following code.

if (expression1) {
	result = 1;
} else if (expression2) {
	result = 2;
} else if (expression3) {
	result = 3;
} else {
	result = 0;
}

With the following code.

result = (expression1) ? 1 : (expression2) ? 2 : (expression3) ? 3 : 0;

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 the Java program.

#Chained Ternary Operators

We can chain more than one Java ternary operator together. We can do so by having one of the values returned by that ternary operator be another ternary operator. Here is the example of the chained ternary operator in Java.

String data = ...

int output = data == null ? 0 : data.equals("") ? 0 : Integer.parseInt(data);

So, we can chain the conditions using the ternary operator.

That’s all for a quick roundup of the ternary operator in Java example.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.