The ternary conditional operator ?: used to define expressions in Java. This is a condensed form of the if-else statement that also returns a value.
In this tutorial, we will see how to use the ternary conditional operator. We’ll start with its syntax and then explore its use.
Syntax:
The ternary operator ?: in Java is the only operator that accepts three operands:
1 2 3 | booleanExpression ? expression1 : expression2 |
The first operand must be a Boolean expression, the second and third operands can be any expression that returns a value. The ternary operator returns instruction1 as an output if the first operand evaluates to true, otherwise instruction2.
Example:
Let’s look at the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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"); } } } |
In the code above, we assigned a value to str based on the conditional evaluation of n. We can make this code more readable and clearer by easily replacing the if-else statement with a ternary condition:
1 2 3 4 5 6 7 8 9 10 11 12 | 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); } } |
Ternary Operator in Java with Multiple Conditions
You can replace multiple lines of code with a single line of code using the ternary operator. This makes your code more readable. For example, you can replace the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | class Main { public static void main(String[] args) { int number = 3; String str; if (number == 1) { str = "A"; } else if (number == 2 ) { str = "B"; } else if (number == 3) { str = "C"; } else { str = "X"; } System.out.println(str); } } |
with:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class Main { public static void main(String[] args) { int number = 3; String str; str = (number == 1) ? "A" : (number == 2) ? "B" : (number == 3) ? "C" : "X"; System.out.println(str); } } |
The use of the ternary operator made the code more difficult to understand in this case. Use the ternary operator only when the resulting instruction is short. This makes the code more concise and much more readable.
Ternary Operator Examples
Example 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | class Main { public static void main(String[] args) { int num =5; final String msg = num > 10 ? "Number is greater than 10" : "Number is less than or equal to 10"; System.out.println(msg); } } |
Output:
1 2 3 | Number is less than or equal to 10 |
Example 2:
It’s possible for us to nest our ternary operator to any number of levels of our choice. So the construct:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class Main { public static void main(String[] args) { int num =6; String msg = num > 10 ? "Number is greater than 10" : num > 5 ? "Number is greater than 5" : "Number is less than equal to 5"; System.out.println(msg); } } |
is valid in Java. To improve the readability of the above code, we can use braces (), wherever necessary:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class Main { public static void main(String[] args) { int num =6; String msg = num > 10 ? "Number is greater than 10" : (num > 5 ? "Number is greater than 5" : "Number is less than equal to 5"); System.out.println(msg); } } |
However, please note that it’s not recommended to use such deeply nested ternary constructs in the real world. This is so as it makes the code less readable and difficult to maintain.