Numeric Promotion Rules
What is the data type of x + y?
double x = 39.21;
float y = 2.1;
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.
Logical Operators
if(hourOfDay < 11)
System.out.println(“Good Morning”);
morningGreetingCount++;
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.
Data types supported by switch statements
Note that boolean and long, and their associated wrapper classes, are not supported by switch statements.