Chapter 2 Flashcards

1
Q

Numeric Promotion Rules

A
  1. If two values have different data types, Java will automatically promote one of the values to the larger of the two data types.
  2. If one of the values is integral and the other is floating-point, Java will automatically promote the integral value to the floating-point value’s
  3. Smaller data types, namely byte, short, and char, are first promoted to int any time they’re used with a Java binary arithmetic operator, even if neither of the operands is int.
  4. After all promotion has occurred and the operands have the same data type, the result- ing value will have the same data type as its promoted operands.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the data type of x + y?

double x = 39.21;
float y = 2.1;

A

This is actually a trick question, as this code will not compile! As you may remember from Chapter 1, floating-point literals are assumed to be double, unless postfixed with an f, as in 2.1f. If the value was set properly to 2.1f, then the promotion would be similar to the last example, with both operands being promoted to a double, and the result would be a double value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Logical Operators

A
  • AND is only true if both operands are true.
  • Inclusive OR is only false if both operands are false.
  • Exclusive OR is only true if the operands are different.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

if(hourOfDay < 11)
System.out.println(“Good Morning”);
morningGreetingCount++;

A

Based on the indentation, you might be inclined to think the variable morningGreeting- Count is only going to be incremented if the hourOfDay is less than 11, but that’s not what this code does. It will execute the print statement only if the condition is met, but it will always execute the increment operation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Data types supported by switch statements

A
  • int and Integer
  • byte and Byte
  • short and Short
  • char and Character int and Integer String
  • enum values

Note that boolean and long, and their associated wrapper classes, are not supported by switch statements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly