The ternary operator is a conditional operator that is used in Java programming language. It is expressed as ?:
which is very strange for Java syntax. The ternary operator provides some shortcuts and elegant solutions for different conditional situations.
What Is the Ternary Operator?
The ternary operator provides a condensed form of the if-else statement. Ternary operator can be used for selection condition with just two choices. For multiple-choice select if-else or switch-case can be used.
Syntax
The syntax of the ternary operator is a bit different.
CONDITION? EXPRESSION1 : EXPRESSION2
- `CONDITION` is an expression that simply returns a boolean value like false or true. This is used to evaluate given expression and according to the result, the EXPRESSION1 or EXPRESSION2 will be executed. If the evaluated expression is true the EXPRESSION1 will be executed if the evaluated expression is false EXPRESSION2 will be executed.
- `EXPRESSION1` is an expression where it will be executed if CONDITION returns true.
- `EXPRESSION2` is an expression where it will be executed if CONDITION returns false.
Ternary Operator Example
We will start with a simple ternary operator example. We will check the boolean expression number < 5
and if it is true we will print number variable is lower than 5
if it is false we will print number variable is higher than 5
.
public class Ternary_Operator { public static void main(String[] args) { int number = 3; String result1 = number < 5 ? "number variable is lower than 5" : "number variable is higher than 5"; System.out.println(result1); //Output will be "number variable is lower than 5" number = 8; String result2 = number < 5 ? "number variable is lower than 5" : "number variable is higher than 5"; System.out.println(result2); //Output will be "number variable is higher than 5" } }
In this example, the result1
will be number variable is lower than 5
because the number > 5
will be evaluated to the true
where the ternary expression will return the first expression.
The result2
will be number variable is higher than 5
because the number < 5
will be evaluated to the false
because 8<5
will return false. This will cause the ternary expression to return the second expression.
Nested or Chained Ternary Operator Example
Multiple ternary operators can be nested or chained together. Even it is possible to nest multiple ternary operators it is not recommended which will make reading the code harder and error-prone. Below is a simple nested ternary operator example for Java.
public class Ternary_Operator { public static void main(String[] args) { int number = 8; String result = number > 10 ? "Number is greater than 10" : (number > 5 ? "Number is greater than 5":"Number is less than 5"); System.out.println(result); } }
Ternary Operator Null Check
We can make a null value check by using the ternary operator easily. This is useful when we check before calling a method.
public class Ternary_Operator { public static void main(String[] args) { Object myobject; String value = myobject!= null ? object.getValue():null; } }
This code will check if the myobject is null or not. If the myobject is not null then getValue()
function will be called and the return value will be set to value
. But if the myobject
is null the value
will be set as null
. In this case, we can see that myobject
is null and the null
value will be set to the value
.
The following code is the equivalent of the previous ternary operation.
public class Ternary_Operator { public static void main(String[] args) { String value = null; if(object != null) { value = object.getValue(); } } }
Ternary Operator As max Function
As another example, the ternary operator can be used like a max() function which will return the maximum value for the given two values.
public class Ternary_Operator { public static void main(String[] args) { int value1 = 10; int value2 = 20; int max = value1 >= value2 ? value1 : value2; } }
Ternary Operator As min Function
Like max() function the ternary operator can be used as min()
function which will return or select the lower value of two provided values. If two values are the same the value1
will be returned as both values are the same.
public class Ternary_Operator { public static void main(String[] args) { int value1 = 10; int value2 = 20; int max = value1 <= value2 ? value1 : value2; } }
Ternary Operator As abs Function
Another interesting usage of the ternary operator is using it like an abs()
function which will return the absolute value of the given value which can be a positive or negative value.
public class Ternary_Operator { public static void main(String[] args) { int value = -5; int abs_value = value >= 0 ? value : -value; } }