int anInt = 1;
double aDouble = 2.5;
anInt = anInt + aDouble; // Error - need to cast double to int
anInt += aDouble; // This is ok. Why?
anInt = aDouble; // This is also an error.
anInt = 1 + aDouble; // This is also an error.
So my questions is: Why is it not a compile error to do anInt += aDouble?
Three of the four cases properly report an error. Compound assignment is the only exception from the rule. Java Language Specification, part 15.26.2, explains why:
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);
As you can see, the error is avoided by implicit insertion of a cast.
Related
This question already has answers here:
Why don't Java's +=, -=, *=, /= compound assignment operators require casting?
(11 answers)
Closed 5 years ago.
i know that by default numbers are stored as integer in java but
byte x = 10;
x = x + 10;
is giving error while
byte x = 10;
x += 10;
is compiling fine
JLS have an answer for you
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.
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);
So in your case your second statement equlas to
x = (byte) x + 10;
That is the reason compiler is happy about.
I got my sample exam question in java. I saw this expression sum += sum + d in the for loop.
Here is the code:
double sum =0;
for (double d = 0; d<10; sum += sum + d) {
d += 0.1;
}
I just dont understand that part.
I only know these:
x+=1
x=x + ++x;
x=x + x++;
Thanks in advance!
(I'm glad you understand the three obfuscated statements. But please don't use them in production. The final two are undefined in C and C++).
sum += a is shorthand for sum = sum + a for any a (neglecting any subtle differences due to implicit type conversions).
So sum += sum + d is sum = sum + sum + d; which simplifies to
sum = 2 * sum + d;
If to be more accurate, sum += a is not same to sum = sum + a, there is a type cast to type of sum. Let's consider the next example:
short x = 3;
x += 4.6;
It's the same to
short x = 3;
x = (short)(x + 4.6);
But not to x = x + 4.6
So, we have type cast to the type of sum. For more details read
JLS:
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.
From the operators precedence, you can see that + (additive) operator has higher precedence than += (assignment) operator, so we first evaluate the sum + d part, let's call this result sumplusd, we get :
sum += sumplusd
Then we evaluate +=, that is increment the variable on the left part of the expression by the value on the right part, we get :
sum = sum + sumplusd, which reads sum = sum + (sum + d), which reads sum = sum*2 + d .
To simplify the whole mess, including the do loop:
double sum = 0.0;
double d = 0.0;
while ( d < 10.0 )
{
sum = 2.0*sum + d
d = d + 0.1;
}
Since, sum + = "any variable or constant" is equivalent to sum = sum + "variable or constant". Thus, sum+=sum+d would be same as sum = sum + ( sum + d ).
This question already has answers here:
Difference between a += 10 and a = a + 10 in java? [duplicate]
(5 answers)
Closed 8 years ago.
I've been told that there are differences between a+=b; and a=a+b; which can result in only one of those being legal depending on the type declerations.
Does anyone have an example of this?
There is basically no difference, however there is a subtle difference.
The arithmetic assignment operators do an implicit cast. e.g.
byte a = 1;
int b = 2;
a += b; // compiles
a = a + b; // doesn't compile as a byte + int = int
a = (byte) (a + b); // compiles as this is the same as +=
For more weird examples.
int a = 5;
a += 1.5f;
// a == 6
char ch = '0'; // (char) 49
ch *= 1.1; // ch = '4';
long l = Integer.MAX_VALUE;
l += 0.0f; // i = (long ) ((long ) l + 0.0f)
// i == Integer.MAX_VALUE + 1L; WTF!?
// l is no longer Integer.MAX_VALUE due to rounding error.
The JLS (section 15.26.2) says:
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.
The presence of the type-cast means that there are a couple of edge cases where a = a op b means something different to a op= b.
See Peter Lawrey's answer for one example. It is when a is a byte and b is an int and the "op" is +. The "gotcha" is a + b produces an int, which then can't be assigned to a ... without a typecast.
The same scenario applies with other types for a and b and for other arithmentic and bitwise operators.
int a = 10;
int b = 20;
a=a+b; // 30
a+=b; // 30
System.out.println(a);
Both will give the same answer.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java += operator
Code example:
double d = 1;
float f = 2;
f += d; // no error?
f = f+d; // type mismatch error, should be f = (float) (f+d);
So why does f+=d not produce an error (not even at runtime), although this would decrease the accuracy of d?
As per JLS 15.26.2
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.
That means:
f += d;
would become as
f = (float) (f+d);
The compount assignment does an implicit cast.
a #= b;
is equivalent to
a = (cast to type of a) (a # b);
Another example
char ch = '0';
ch *= 1.1; // same as ch = (char)(ch * 1.1);
// ch is now '4'
I have an expression with compound assignment as
x += 2*5
so how it will be evaluated is it
x = (x + 2) * 5
or
x = x + (2 * 5)
and why?
An expression of the form
x += expr;
is equivalent to
x = x + (expr);
So in this case it's
x = x + (2 * 5);
It would be very weird and confusing if the current value of x was used for part of the expression implicitly.
Any expression in the form of var op= expr is evaluated to var = var op expr. This is defined in the java specification 15.26.2 Compound Assignment Operators.
I see the phrase "operator precedence" mentioned, and I believe this is confusing the issue. Consider this:
x *= a + b;
Even though * has a higher precedence than +, this is still evaluated as
x = x * (a + b)
The full explanation is given in JLS 15.26.2 Compound Assignment Operatos:
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