What is the data type of x + y?
double x = 39.21;
float y = 2.1;
Explanation:
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.
But I don't understand. If float y = 2.1; was assumed to be double there would be no need for the promotion of variable y to the double. And I'm more confused by the next problem, which is:
What is the data type of x * y / z?
short x = 14; float y = 13; double z = 30;
The book says this will compile even the float y = 13; is not float y = 13f. Do I only add f next to the float number if they are decimal? I really can't see the difference between this problem and aforementioned problem.