Flylib.com

Books Software

 
 
 

3.15 The Conditional Operator: ?


3.15 The Conditional Operator: ?

The ternary conditional operator allows conditional expressions to be defined. The operator has the following syntax:


<condition>   ?   <expression 1 >   :   <expression 2 >

If the boolean expression <condition> is true then <expression 1 > is evaluated; otherwise , <expression 2 > is evaluated. Of course, <expression 1 > and <expression 2 > must evaluate to values of compatible types. The value of the expression evaluated is returned by the conditional expression.

boolean leapYear = false;
int daysInFebruary = leapYear ? 29 : 28;   // 28

The conditional operator is the expression equivalent of the if-else statement. The conditional expression can be nested and the conditional operator associates from right to left:


(a?b?c?d:e:f:g)   evaluates  as   (a?(b?(c?d:e):f):g)


3.16 Other Operators: new , [] , instanceof

The new operator is used to create objects, that is, instances of classes and arrays. It is used with a constructor call to instantiate classes (see Section 4.4, p. 117), and with the [] notation to create arrays (see Section 4.1, p. 100). It is also used to instantiate anonymous arrays (see Section 4.1, p. 104), and anonymous classes (see Section 7.5, p. 308).

Pizza onePizza = new Pizza();       // Create an instance of Pizza class.

The [] notation is used to declare and construct arrays and also to access array elements (see Section 4.1, p. 100).

int[] anArray = new int[5];// Declare and construct an int array of 5 elements.
anArray[4] = anArray[3];   // Element at index 4 gets value of element at index 3.

The boolean, binary, and infix operator instanceof is used to test an object's type (see Section 6.6, p. 264).

Pizza myPizza = new Pizza();
boolean test1 = myPizza instanceof Pizza;  // True.
boolean test2 = "Pizza" instanceof Pizza;  // Compile error. String is not Pizza.
boolean test3 = null instanceof Pizza;     // Always false. null not an instance.

Review Questions

graphics/rq_icon.gif
3.20

What would be printed during execution of the following program?

public class MyClass {
    public static void main(String[] args) {
        test(1<<32, "1<<32");
        test(1<<31, "1<<31");
        test(1<<30, "1<<30");
        test(1,     "1"    );
        test(0,     "0"    );
        test(-1,    "-1"   );
    }

    public static void test(int i, String exp) {
        if ((i >> 1) != (i >>> 1)) System.out.println(exp);
    }
}

Select the two correct answers.

  1. "1<<32"

  2. "1<<31"

  3. "1<<30"

  4. "1"

  5. "0"

  6. "-1"

3.21

Which of the following are not operators in Java?

Select the two correct answers.

  1. %

  2. <<<

  3. &

  4. %=

  5. >>>

  6. <=

  7. &&=

3.22

Given a variable x of type int (which may contain a negative value), which are correct ways of doubling the value of x , barring any wrapping of out-of-range intermediate values?

Select the four correct answers.

  1. x << 1;

  2. x = x * 2;

  3. x *= 2;

  4. x += x;

  5. x <<= 1;

3.23

Which of the following operators can be used both as an integer bitwise operator and a boolean logical operator?

Select the three correct answers.

  1. ^

  2. !

  3. &

  4. ~

3.24

Given these declarations, which of the following expressions are valid?

byte  b = 1;
char  c = 1;
short s = 1;
int   i = 1;

Select the three correct answers.

  1. s = b * 2;

  2. i = b << s;

  3. b <<= s;

  4. c = c + b;

  5. s += i;