SCJP Study Guide:
Fundamentals


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend


Previous Next vertical dots separating previous/next from contents/index/pdf Contents
XyzWs SCJP Study Guide: Conditional Operator

Conditional Operator


The ?: operator is called the conditional operator. It is at times also called the ternary operator. The conditional operator is unique because it is trinary (three operands) and because it returns values of all types. It can return a numeric value, a string, a Boolean value, and so on.

The conditional operator ? : uses the boolean value of one expression to decide which of the two other expressions should be evaluated.

exp1 ? exp2 : exp3

A conditional expression of the form exp1 ? exp2 : exp3 first evaluates the condition exp1. Then, if exp1 is true, exp2 is evaluated and becomes the result of the operation. Otherwise, exp3 is evaluated and becomes the result of the operation. A conditional expression never evaluates both exp2 and exp3. Only one of two expressions is ever evaluated.

In other words, the conditional expression is sort of an if/else statement buried inside of an expression. For example:

if (x == 5) {
   str = "abc";
}
else {
   str = "def";
}

It is the same as the following code:

str = (x == 5)?"abc":"def";

The conditional operator is right-associative, meaning that operations are grouped from right to left. For example, an expression of the form a ? b : c ? d : e is evaluated as a ? b : (c ? d : e).

The first expression must be of type boolean or Boolean, or a compile-time error occurs.

Note that it is a compile-time error for either the second or the third operand expression to be an invocation of a void method. For example:

class Program {
  static void doSomethingX() { }
  static void doSomethingY() { }
  pubic static void main(String[] args) {
    int x;
    ...
    System.out.println((x==5)?doSomethingX():doSomethingY()); //compile-time error
   
  }
}

In fact, it is not permitted for a conditional expression to appear in any context where an invocation of a void method could appear.

The type of a conditional expression is determined as follows:

  • If the second and third operands have the same type (which may be the null type), then that is the type of the conditional expression.
  • If one of the second and third operands is of type boolean and the type of the other is of type Boolean, then the type of the conditional expression is boolean.
  • If one of the second and third operands is of the null type and the type of the other is a reference type, then the type of the conditional expression is that reference type.
  • Otherwise, if the second and third operands have types that are convertible to numeric types, then there are several cases:
    • If one of the operands is of type byte or Byte and the other is of type short or Short, then the type of the conditional expression is short.
    • If one of the operands is of type T where T is byte, short, or char, and the other operand is a constant expression of type int whose value is representable in type T, then the type of the conditional expression is T.
    • If one of the operands is of type Byte and the other operand is a constant expression of type int whose value is representable in type byte, then the type of the conditional expression is byte.
    • If one of the operands is of type Short and the other operand is a constant expression of type int whose value is representable in type short, then the type of the conditional expression is short.
    • If one of the operands is of type; Character and the other operand is a constant expression of type int whose value is representable in type char, then the type of the conditional expression is char.
    • Otherwise, binary numeric promotion is applied to the operand types, and the type of the conditional expression is the promoted type of the second and third operands. Note that binary numeric promotion performs unboxing conversion and value set conversion.
  • Otherwise, the second and third operands are of types S1 and S2 respectively. Let T1 be the type that results from applying boxing conversion to S1, and let T2 be the type that results from applying boxing conversion to S2. The type of the conditional expression is the result of applying capture conversion to lub(T1, T2).

At run time, the first operand expression of the conditional expression is evaluated first; if necessary, unboxing conversion is performed on the result; the resulting boolean value is then used to choose either the second or the third operand expression:

  • If the value of the first operand is true, then the second operand expression is chosen.
  • If the value of the first operand is false, then the third operand expression is chosen.

The chosen operand expression is then evaluated and the resulting value is converted to the type of the conditional expression as determined by the rules stated above. This conversion may include boxing or unboxing conversion. The operand expression not chosen is not evaluated for that particular evaluation of the conditional expression.

 


Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |