Ternary Operator In Java Tutorial with Examples – POFTUT

Ternary Operator In Java Tutorial with Examples

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 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.

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.

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.

echo '
'

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.

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.

LEARN MORE  Awk If, If Else , Else Statement or Conditional Statements

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *