0

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.

Andy Turner
141k11 gold badges169 silver badges263 bronze badges
asked May 18, 2017 at 21:37
3
  • 4
    y is allocated as a float. The literal expression "2.1" (without an explicit trailing f) is a double. It won't compile because you can't stuff a larger double into a smaller float. Commented May 18, 2017 at 21:41
  • 1
    You can, however, assign an int 13 into a float, because an int can be promoted to a float. Commented May 18, 2017 at 21:42
  • Related: stackoverflow.com/questions/28361456/… Commented May 18, 2017 at 21:47

3 Answers 3

2

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

answered May 18, 2017 at 22:20
Sign up to request clarification or add additional context in comments.

Comments

0

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.

answered May 18, 2017 at 21:44

1 Comment

"the float is an integer"? Did you mean "the value is an integer"
0

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);
answered May 18, 2017 at 21:50

3 Comments

You are correct, I have miss interpreted my understanding. Excuse me for this.
But if the value of y is treated as a double, then shouldn't x+y still work since it's double + double?
@MadPhysicist answer correctly explains it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.