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.
3 Answers 3
Ignoring char, Java will promote numeric types like this:
byte > short > int > long > float > double
These are called widening conversions. See JLS §5.1.2. Widening Primitive Conversion for detail.
Binary operators will promote to int, long, float, or double, whichever is nearest from the two values of the operator, i.e. result will never be byte or short. Example: byte + short will promote both sides to int. See JLS §5.6.2. Binary Numeric Promotion for detail.
The assignment operator will also do widening conversion of the value, with the extra rule that a constant expression of type byte, short, or int will go through a narrowing conversion if the type of the variable is byte or short, and the value of the constant expression is representable in the type. Note, there is no rule for narrowing double constant to float. See JLS §5.2. Assignment Contexts for detail.
So, for your code:
double x = 39.21; // double constant to double (identity conversion)
float y = 2.1; // fails because double constant cannot promote to float
If code had compiled, what is the data type of x + y?
x + y // double + float promotes to double
Answer: double
Next part:
short x = 14; // narrowing conversion of int constant to short
float y = 13; // widening conversion of int constant to float
double z = 30; // widening conversion of int constant to double
Now, what is the data type of x * y / z?
x * y // short * float promotes to float
(x * y) / z // (float) / double promotes to double
Answer: double
Comments
For the first question, you cannot initialize a float(with a double value) as a double value. Using the 2.1f initializes that value as a float. So you would need y = 2.1f. You can read more about that here
For the second question, the reason this works is the float is an integer (13) which can be promoted to type float.
1 Comment
As mentioned in this post, if you do not specify the character 'f' after the digits, the value will be treated as a double.
With that said, the piece of code below will compile but only if the character 'f' is added to the end of the floating point value.
double x = 7.0;
float y = 2.0f;
System.out.print(x + y);
3 Comments
Explore related questions
See similar questions with these tags.
13into a float, because an int can be promoted to a float.