Java ternary operator

Literal meaning of ternary is three. As the name suggests, a ternary operator is an operator which has three entities or operands.
First of these three operands is a condition. This condition should return either true or false while second and third operands are also expressions.

The ternary operator returns one of these(either second or third operand) as the result based on whether the first operand(the condition or boolean expression) is true or false.
A ternary operator can be used as a replacement of simple if-else statement having a single statement.

Java ternary operator syntax
A ternary operator is a conditional operator having three operands and these operands are separated with ? and :
Syntax of a ternary operator is as follows

Operand 1 ? Operand 2 : Operand 3

Operand 1 is an expression which should return true or false.
If it evaluates to true, then the ternary operator returns Operand 2(expression after ?).
If Operand 1 evaluates to false, then the ternary operator returns Operand 3(expression after : (colon)).

Remember that all the three operands or expressions should return a value and there must be a variable at left hand side to collect or store this value. An example will make it more clear.

Java ternary Operator Example
Below is an example program showing the usage of ternary operator in java.

public class TernaryExample {
  public static void main(String[] args) {
    String shorthand = "java";
    String langOne = "java";
    String langTwo = "javascript";
    String language = "js".equals(shorthand) ? langTwo : langOne;
    System.out.println("Language is " + language);
  }
}

Above program contains a ternary operator with the first expression as a condition which compares the value of shorthand variable with “js”.
Since the result is false, the ternary operator returns the expression after : which is assigned to a variable.
The program prints below result.

Language is java

If the comparison would have resulted in true, then the return value of the ternary operator would have been the expression after ?, that is, “javascript”.

Condition(or operand 1) of ternary operator need not be a simple condition.
It can be a complex condition with logical operators such as && or ||. Example,

(shorthand != null && "js".equals(shorthand)) ? languageTwo : languageOne;

Java ternary Operator Vs if statement
As stated earlier, a ternary operator can be used as a replacement of an if-else statement.
Using a ternary operator makes the code more readable, simpler and shorter, provided that it is not over used.

Thus, in the above example, the ternary operator when replaced with an if-else will look like

String language = "";
if("js".equals(shorthand)) {
  language = "javascript";
} else {
  language = "java";
}

You can see the difference.
A ternary operator can not be replaced with an if-else statement when either if block or else block has multiple statements to execute.
A ternary operator is only suitable when there is a single statement to execute or when a choice is to be made out of two.

Nested ternary operator
Using a ternary operator inside another ternary operator is called nesting of ternary operators.
Inner ternary operator is used in place of return values(that in, place of Operands 2 or 3 or both) of the outer ternary operator.
Example,

public class TernaryExample {
  public static void main(String[] args) {
    int number = 50;
    String size = number > 30 ? number > 60 ? "Biggest" : "Bigger" :"Big";
    System.out.println("Number is " + size); // result will be "Bigger
  }
}

Above example has a ternary operator with the first operand as a condition that compares a number with 30.
If it is greater, then the control will move to the inner ternary operator(that is Operand 2) which compares the number with 60.
This is the condition of the inner ternary operator.

If the condition of the inner ternary operator is true, then the result will be “Biggest” else “Bigger”.

If the condition of the outer ternary operator is false, then the inner ternary operator will never be executed and the result will beĀ “Big”.
Nested ternary operator in the above example when converted to an if-else block will look as below.

String size = "";
if(number > 30) {
   if(number > 60) {
      size = "Biggest";
   } else {
      size = "Bigger";
   }
} else {
   size = "Big";
}

A nested ternary operator can also be enclosed between parenthesis to enhance readability.
Thus, the ternary operator used in the above example could also be written as

String size = number > 30 ? (number > 60 ? "Biggest" : "Bigger") :"Big";

That is all on this article on ternary operators in java. Hit clap below if you liked it.

Leave a Reply