Assignment Conversion

P

Philipp

Hello,
In the following code the first assignment compiles OK but the second
doesn't. I am surprised that the first is OK, but couldn't find the
exact ref in the JLS which specifies this conversion/promotion.
Could someone point me to the right part of the JLS? Thanks Phil

public class ATest {
public static void main(String[] args) {
float f = 12.3f;
double d = 32.1;
f += d; // is OK
f = f + d; // does not compile
}
}
 
S

Stefan Ram

Philipp said:
couldn't find the exact ref in the JLS which
specifies this conversion/promotion.

This is given directly at the start of the
section where one would expect it, i.e.,
the section treating those operators.
 
R

Roedy Green

f += d; // is OK
f = f + d; // does not compile

the += operators are "tough". They seem to have a built in (float) in
them. The + operators want the explicit cast to narrow the double
back to a float.

Similarly you can say

byte b;
..

b++;
b += 1;
// but
b = (byte)(b + 1);
--
Roedy Green Canadian Mind Products
http://mindprod.com
Your old road is
Rapidly agin'.
Please get out of the new one
If you can't lend your hand
For the times they are a-changin'.
 
J

Joshua Cranmer

Philipp said:
Hello,
In the following code the first assignment compiles OK but the second
doesn't. I am surprised that the first is OK, but couldn't find the
exact ref in the JLS which specifies this conversion/promotion.
Could someone point me to the right part of the JLS? Thanks Phil

JLS §15.26.2 Compound Assignment Operators
A compound assignment expression of the form E1 op= E2 is equivalent to
E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is
evaluated only once.

JLS §15.26.1 Simple Assignment Operator
A compile-time error occurs if the type of the right-hand operand cannot
be converted to the type of the variable by assignment conversion (§5.2).

The common representation of the compound assignment is incorrect. |a +=
b| is not the same as |a = a + b| but rather |a = (<type of a>)(a + b)|.
 
O

Owen Jacobson

the += operators are "tough". They seem to have a built in (float) in
them.  The + operators want the explicit cast to narrow the double
back to a float.

Similarly you can say

     byte b;
     ..

     b++;
     b += 1;
     // but
     b = (byte)(b + 1);

The built-in cast in the += family of operators is mandated by the
JLS:
15.26.2 Compound Assignment Operators

A compound assignment expression of the form E1 op= E2 is equivalent
to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1
is evaluated only once. For example, the following code is correct:

short x = 3;
x += 4.6;

and results in x having the value 7 because it is equivalent to:

short x = 3;
x = (short)(x + 4.6);

-o
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top