Ternary Operator in Java
In some cases, the if...else expression in Java can be replaced by a ternary operator. Visit the Java if...else statement first before learning about the ternary operator.
Ternary operator in Java
A ternary operator is a kind of conditional operator used in Java. With appropriate examples, we shall take a glance about Java's ternary operator in this section.
The word "ternary" has three different meanings. Three operands make up the ternary operator (?:). To evaluate Boolean expressions, it is employed. The choice of value for the variable is made by the operator. In terms of accepting three operands, it's the only conditional operator. It can take the place of the if-else clause. The code is simplified, easier to read, and shorter as a result.
Syntax:
variable = (condition) ? expression1 : expression2
According to the preceding statement, if the condition evaluates to true, expression 1 is run; otherwise, expression 2 is executed, and the end result is saved in a variable.
Example of ternary operator
public class TernaryOperatorExample
{
public static void main(String args[])
{
int x, y;
x = 20;
y = (x == 1) ? 65: 90;
System.out.println("Value of y is: " + y);
y = (x == 20) ? 65: 90;
System.out.println("Value of y is: " + y);
}
}
Output :
Value of y is: 90
Value of y is: 65
Let's look at another example where the ternary operator is used to determine which of three values is the largest.
LargestNumberExample.java
public class LargestNumberExample
{
public static void main(String args[])
{
int x=45;
int y=32;
int z=75;
int largestNumber= (x > y) ? (x > z ? x : z) : (y > z ? y : z);
System.out.println("The largest numbers is: "+largestNumber);
}
}
Output :
The largest numbers is: 75
We've used three variables with values of 69, 89, and 79 in the aforementioned program: x, y, and z. The statement (x > y)? The formula (x > z? x: z): (y > z? y: z) determines which of three numbers is the largest and stores the result inside the variable largestNumber. Let's examine the expression's execution hierarchy.
It first validates the statement (x > y). The expression (x > z? x: z) is executed if it returns true; else, the expression (y > z? y: z) is executed.
It first validates the statement (x > y). Whenever the expression (x > z? x: z) is run, it tests the condition x > z to see if it returns. The value of x is returned if the condition gets true; else, the result of z is returned. If the statement (x > z? x: z) is true, then the statement (y > z? y: z) is true.
When the equation (y > z? y: z) is run, the assumption that y > z is further verified. A value of y is delivered if the condition gets true, otherwise the result of z is returned.
As a result, we may use the ternary operator to find the greatest of the three numbers.
When is the Ternary Operator appropriate?
It Is used for if – else statements,
class Main {
public static void main(String[] args) {
// establish a variable
int number = -13;
if(number > 0) {
System.out.println ("Positive Number");
}
else {
System.out.println ("Negative Number");
}
}
}
Output :
-ve Number
Nested Ternary Operators
One ternary operator can also be used inside of another ternary operator.
Here, is a program to display ternary operators in java
class Main {
public static void main(String[] args) {
// create a variable
int n1 = -22 n2 = 32, n3 = 21;
// nested ternary operator
// for finding the largest number
int largest = (n1 >= n2) ? ((n1 >= n3) ? n1 : n3) : ((n2 >= n3) ? n2 : n3);
System.out.println("Largest Number: " + largest);
}
}
Output :
Largest Number: 32
Here,
- The first test condition is (n1 >= n2), which determines whether n1 is bigger than n2.
- If the first condition is true, the second test condition (n1 >= n3) is carried out.
- If the initial condition becomes false, the third test condition, (n2 >= n3), is carried out.