In this section we will learn what the Ternary operator is and how to use it in Java.
Ternary Operators in Java
Let’s say you want to store one of two values in a variable, but deciding on what value should be stored in the variable is dependent on a condition! For example, there’s an employee and we want to set its salary. If the employee is a manager then the value for its salary is 10000 but if it’s a developer, then its salary is 8000.
This is where we can use ternary operator.
Basically, this operator takes one condition and two values. If the condition was true, it will return the first value but if the result of the condition was false, it will return the second value.
Java Ternary Operator Syntax
This is the structure of the ternary operator:
condition ? expression_True : expression_False;
`condition`: the condition is the one that we declare to either return true or false result.
`expression_true`: This is the first value we set for the ternary operator and it will be returned if the result of the condition is true.
`expression_false`: This is the second value of the ternary operator and of course it will be returned only if the result of the condition becomes false.
`?`: between the condition and the first value we use question mark `?` to separate them from each other.
`:`: also between the first and second value, we use colon `:` to separate these two values from each other as well.
Example: Using Ternary Operator in Java
public class Simple { public static void main(String[] args) { String emp = "Manager"; int salary = emp == "Manager"? 10000:8000; System.out.println("The salary is: "+ salary); } }
Output: The salary is 10000
How does ternary operator work in Java?
In this example, the value of the `salary` variable is dependent on the value of the `emp` variable.
If the value of the `emp` is `Manager` then the value 10000 will be stored in the `salary` variable. But if the value of the `emp` wasn’t `Manager` then the value 8000 is the one to be stored in the `salary` variable.
Example: Ternary Operator as Null Check
class Main{ public static void main(String[] args) { String name = null; String value = name == null? "The default name: John":name; System.out.println(value); } }
Output:
The default name: John
Example: Ternary Operator as max Function
class Main{ public static void main(String[] args) { int val1 = 10; int val2 = 20; int res = val1> val2? val1:val2; System.out.println("Between the val1 and val2 the greater value is: "+ res); } }
Output:
Between the val1 and val2 the greater value is: 20
Example: Ternary Operator as min Function
class Main{ public static void main(String[] args) { int val1 = 10; int val2 = 20; int res = val1< val2? val1:val2; System.out.println("Between the val1 and val2 the smaller value is: "+ res); } }
Output:
Between the val1 and val2 the smaller value is: 10
When to use the Ternary Operator?
The ternary operator is like an if-else statement but in a short version. We mainly use this operator, when we want to assign a value to a variable but first want to compare that value with another value and then based on the result, decide which value should be assigned to the target variable.