The Conditional Operator results in a value based on whether a Boolean expression is evaluated to true or false. The conditional operator consists of a question mark (?) and a colon (:), as follows:
<Boolean expression> ? <expression if true> : <expression if false>
Conditional Operator consists of three expressions:
<Boolean expression> ? <expression 1> : <expression 2>
The first expression is a Boolean expression
expression 1 and expression 2 can be any expression:
- A value
- A non-void method call
- Another conditional operator expression
Example 1: Even or Odd
To determine whether x is even or odd, we can use the mod operator (%) to determine whether x is divisible by 2. If the result of x % 2 is 0, then x is even otherwise, it is odd.

To re-write this if statement using conditional operators, we will put the boolean expression in first.

The ? operator is used after the boolean expression. Think of it as asking if the boolean expression is true. If it is true, then the next part of the expression is evaluated and Even is assigned to result.

A colon (:) is used to separate what happens when the boolean expression is true and when it evaluates to false. If the boolean expression evaluates to false in this example, Odd is assigned to result.

Your Turn
Let's try it in the Java Playground.
- Predict the output of the code.
- Run the code to determine if your prediction is correct.
- Note that the
if statement and conditional operator produce the same result.
- Update the value of
x to an even number and re-run your code.
We could use the conditional operator to print whether the value assigned to x is even or odd simply by putting the expression inside a IO.println statement:
Example 2: Calling Methods
Consider a class called Calculator that has methods to calculate the sum and positive difference of two values as follows:
public double sum(double value1, double value2);
public double posDifference(double value1, double value2);
Your Turn
Let's try it in the Java Playground.
- Based on a String input of
+ for sum and +- for positive difference, we can call the appropriate method.
- Predict the output for the code.
- Run the code to determine if your prediction is correct.
- Modify the values of
x, y, and input to test the code for posDifference as well.
- Predict the output of the code.
- Run the code to determine if your prediction is correct.
Example 3: Nested Conditional Operator
- Expanding on the last example, we have added
product and quotient methods to Calculator as well.
- Predict the output for the code.
- Run the code to determine if your prediction is correct.
- Modify the values of
x, y, and input to test the code for the other methods as well.
- Predict the output of the code for each change and run the code to determine if your prediction is correct.
if..else Equivalent
- The following is a re-write of the above example using an if..else statement.
- Predict the output of the code.
- Run the code to determine if your prediction is correct.
- Modify the values of
x, y, and input to test the code for the other methods as well.
- Predict the output of the code for each change and run the code to determine if your prediction is correct.
Conditional Operator and void Method Calls
Remember, when using the Conditional Operator, the result is a value. You can not use it to call void methods. For example, consider a Robot class that has methods for turning the robot to the left and right as follows:
public void turnRight()
public void turnLeft()
Assuming randy is a properly declared Robot and direction is an int variable, we can NOT write any of the following:
(direction == 39) ? randy.turnRight() : randy.turnLeft();
boolean b = (direction == 39) ? randy.turnRight() : randy.turnLeft();
IO.println ((direction == 39)
? randy.turnRight() : randy.turnLeft());
Resources
Next Learn Tutorial