© Doug Winnie 2021
D. WinnieEssential Java for AP CompScihttps://doi.org/10.1007/978-1-4842-6183-5_31

31. The Ternary Operator

Doug Winnie1  
(1)
Mission Hills, KS, USA
 

One of the most common actions when creating conditional statements is to generate a unique value if a condition evaluates as a Boolean true or false. To make this more concise, a unique operator called the ternary operator allows you to perform a conditional action and generate a unique value that you can use in your program in a single line of code.

The if-else Statement Equivalent

Take the following code as an example of a simple if...else statement that assigns a value:
int result;
boolean test = false;
if (test) {
    result = 1;
} else {
    result = 0;
}

In this example, the variable test will contain a Boolean value, and the if statement will test the value. Since the value will be false, the integer variable result is the number 0.

Converting to a Ternary Operator

This can be simplified using the ternary operator, sometimes simplified using the two characters unique to the operator ?:.

The operator starts by evaluating a condition, and if the ? and : characters are present, it triggers the operator. After the conditional, the ? character defines the value that is returned if the condition is true. After the :, the value is returned if the condition is false:
result = conditional ? true option : false option;
Using this, the preceding if...else statement could be rewritten in a single line of code:
result = test ? 1 : 0;

Because the value of test is false, the value after the colon, 0, is then used and is assigned to the variable result. If the value of test is true, the 1 after the question mark is used and assigned to result .

Using the Ternary Operator Inline with Code

While it is most common to assign a value using the ternary operator, you can also use it inline to provide a Boolean option based on a conditional:
System.out.println((value < 5) ? "Value is less than 5" : "Value is not less than 5");

In this example, if value is less than five, the first phrase after the question mark is displayed. If value is not less than five, the second phrase after the color is displayed.

Code Examples

The next page shows examples of capturing input from the user.

This code is also available in the following GitHub repo:

https://github.com/Apress/essential-java-AP-CompSci
public class Main {
    public static void main(String[] args) {
        // Basic example
        int result;
        boolean test = false;
        if (test) {
            result = 1;
        } else {
            result = 0;
        }
        System.out.println("The result is " + result);
        result = test ? 1 : 0;
        System.out.println("The result is " + result);
        // Logic operator example
        int value = 5;
        String msg;
        if (value < 5) {
            msg = "Value is less than 5";
        } else {
            msg = "Value is not less than 5";
        }
        System.out.println(msg);
        msg = (value < 5) ? "Value is less than 5" : "Value is not less than 5";
        System.out.println(msg);
        // Inline example
        System.out.println((value < 5) ? "Value is less than 5" : "Value is not less than 5");
    }
}
/* Output
The result is 0
The result is 0
Value is not less than 5
Value is not less than 5
Value is not less than 5
*/
Listing 31-1

Example ternary operator code

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.29.209