This question already has answers here:
Why don't Java's +=, -=, *=, /= compound assignment operators require casting?
(11 answers)
Closed 6 years ago.
A colleague of mine asked this question to me and I am kind of confused.
int i = 123456;
short x = 12;
The statement
x += i;
Compiles fine however
x = x + i;
doesn't
What is Java doing here?
int i = 123456;
short x = 12;
x += i;
is actually
int i = 123456;
short x = 12;
x = (short)(x + i);
Whereas x = x + i is simply x = x + i. It does not automatically cast as a short and hence causes the error (x + i is of type int).
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.2
The + operator of integral types (int, short, char and byte) always returns an int as result.
You can see that with this code:
//char x = 0;
//short x = 0;
//byte x = 0;
int x = 0;
x = x + x;
It won't compile unless x is an int.
Numbers are treated as int unless you specifically cast them otherwise. So in the second statement when you use a literal number instead of a variable, it doesn't automatically cast it to the appropriate type.
x = x + (short)1;
...should work.
Related
This question already has answers here:
What are the rules for evaluation order in Java?
(5 answers)
Closed 4 years ago.
For the next code, what it's z? (Java)
int x = 5;
int y = 10;
int z =++x*y--;
The order of priority is: y--, ++x, *, =.
( https://introcs.cs.princeton.edu/java/11precedence/ )
Why after run the code, z = 60 ?
The ++ operator is evaluated before the expression.
i.e.:
int x = 10;
int y = ++x; //y = 11
int z = x ++; // z = 11;
y-- is higher up on the list from your source. However, when the post-decrement happens, it happens after the whole evaluation.
So if you print y after getting the value of z, it will be 9.
And the pre-increment happens first, so ++x becomes 6 within that statement (and obviously multiplies by 10).
See an example in the docs.
This question already has answers here:
Why don't Java's +=, -=, *=, /= compound assignment operators require casting?
(11 answers)
Closed 9 years ago.
Here's an oddity:
float a = 0;
a = a + Math.PI; // ERROR
and yet:
a += Math.PI; // OK!
even this works:
int b = 0;
b += Math.PI; // OK, too!
Why does the += operator allow lossy implicit type conversions?
From 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.
Notice that there is a cast involved with the compound assignment. However, with the simple addition there is no cast, hence the error.
If we include the cast, the error is averted:
float a = 0;
a = (float) (a + Math.PI); // works
It's a common misconception that x += y is identical to x = x + y.
That's because
float a = 0;
double b = 1;
a += b;
is the equivalent of
float a = 0;
double b = 1;
a = (float)(a + b);
a += is the same as a = (<type of a>)(a +
For example:
int a = 10;
a += 1.5;
This runs perfectly, but
a = a+1.5;
this assignment says Type mismatch: cannot convert from double to int.
So my question is: what is the difference between += operator and = operator. Why the first assignment didn't says nothing, but second will. Please explain to me. Just I want to know whether I can use the first assignment to all place or not.
From the Java Language Specification section 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.
So the most important difference (in terms of why the second version doesn't compile) is the implicit cast back to the type of the original variable.
int a = 10;
a += 1.5;
is equivalent to:
int a = 10;
a = (int) (a + 1.5);
In general:
x += y; is equivalent to x = (type of x) (x + y);
See 15.26.2. Compound Assignment Operators
Check this link
int a = 10;
a += 1.5;
will be treated as
int a=10;
a=(int)(a+1.5);
As you can found in this link expressions
In case of
a += 1.5;
implicit auto boxing is done
where as here
a = a+1.5;
you are explicitly adding a int variable to a float/double variable
so to correct it
a = a+(int)1.5;
or
a = (int) (a+1.5);
Can you please help me understand what the following code means:
x += 0.1;
The "common knowledge" of programming is that x += y is an equivalent shorthand notation of x = x + y. As long as x and y are of the same type (for example, both are ints), you may consider the two statements equivalent.
However, in Java, x += y is not identical to x = x + y in general.
If x and y are of different types, the behavior of the two statements differs due to the rules of the language. For example, let's have x == 0 (int) and y == 1.1 (double):
int x = 0;
x += 1.1; // just fine; hidden cast, x == 1 after assignment
x = x + 1.1; // won't compile! 'cannot convert from double to int'
+= performs an implicit cast, whereas for + you need to explicitly cast the second operand, otherwise you'd get a compiler error.
Quote from Joshua Bloch's Java Puzzlers:
(...) compound assignment expressions automatically cast the result of
the computation they perform to the type of the variable on their
left-hand side. If the type of the result is identical to the type of
the variable, the cast has no effect. If, however, the type of the
result is wider than that of the variable, the compound
assignment operator performs a silent narrowing primitive
conversion [JLS 5.1.3].
x += y is x = x + y
x -= y is x = x - y
x *= y is x = x * y
x /= y is x = x / y
x %= y is x = x % y
x ^= y is x = x ^ y
x &= y is x = x & y
x |= y is x = x | y
and so on ...
It's one of the assignment operators. It takes the value of x, adds 0.1 to it, and then stores the result of (x + 0.1) back into x.
So:
double x = 1.3;
x += 0.1; // sets 'x' to 1.4
It's functionally identical to, but shorter than:
double x = 1.3;
x = x + 0.1;
NOTE: When doing floating-point math, things don't always work the way you think they will.
devtop += Math.pow(x[i] - mean, 2); will add the result of the operation Math.pow(x[i] - mean, 2) to the devtop variable.
A more simple example:
int devtop = 2;
devtop += 3; // devtop now equals 5
In java the default type of numbers like 2 or -2(without a fractional component) is int and unlike c# that's not an object and we can't do sth like 2.tostring as in c# and the default type of numbers like 2.5(with a fractional component) is double;
So if you write:
short s = 2;
s = s + 4;
you will get a compilation error that int cannot be cast into short also if you do sth like below:
float f = 4.6;
f = f + 4.3;
you will get two compilation errors for setting double '4.6' to a float variable at both lines and the error of first line is logical because float and double use different system of storing numbers and using one instead of another can cause data loss;
two examples mentioned can be changed like this:
s += 4
f += 4.3
which both have an implicit cast behind code and have no compile errors;
Another point worthy of consideration is numbers in the range of 'byte' data type are cached in java and thus numbers -128 to 127 are of type byte in java and so this code doesn't have any compile errors:
byte b = 127
but this one has an error indeed:
byte b = 128
because 128 is an int in java;
about long numbers we are recommended to use an L after the number for the matter of integer overflow like this:
long l = 2134324235234235L
in java we don't have operator overloading like c++ but += is overloaded only for String and not for the let's say StringBuilder or StringBuffer and we can use it instead of String 'concat' method but as we know String is immutable and that will make another object and will not change the same object as before :
String str = "Hello";
str += "World";
It's fine;
You can take a look at the bytecode whenever you want to understand how java operators work. Here if you compile:
int x = 0;
x += 0.1;
the bytecode will be accessible with jdk command javap -c [*.class]:(you can refer to Java bytecode instruction listings for more explanation about bytecode)
0: iconst_0 // load the int value 0 onto the stack
1: istore_1 // store int value into variable 1 (x)
2: iload_1 // load an int value from local variable 1 (x)
3: i2d // convert an int into a double (cast x to double)
4: ldc2_w #2 // double 0.1d -> push a constant value (0.1) from a constant pool onto the stack
7: dadd // add two doubles (pops two doubles from stack, adds them, and pushes the answer onto stack)
8: d2i // convert a double to an int (pops a value from stack, casts it to int and pushes it onto stack)
9: istore_1 // store int value into variable 1 (x)
Now it is clear that java compiler promotes x to double and then adds it with 0.1.
Finally it casts the answer to integer .
There is one interesting fact I found out that when you write:
byte b = 10;
b += 0.1;
compiler casts b to double, adds it with 0.1, casts the result which is double to integer, and finally casts it to byte and that is because there is no instruction to cast double to byte directly.
You can check the bytecode if you doubt :)
devtop += Math.pow(x[i] - mean, 2); adds Math.pow(x[i] - mean, 2) to devtop.
It increases the value of the variable by the the value after +=.
For example:
float x = 0;
x += 0.1;
//x is now 0.1
x += 0.1;
//x is now 0.2
It's just a shorter version of:
x = x+0.1;
I've misplaced += with =+ one too many times, and I think I keep forgetting because I don't know the difference between these two, only that one gives me the value I expect it to, and the other does not.
Why is this?
a += b is short-hand for a = a + b (though note that the expression a will only be evaluated once.)
a =+ b is a = (+b), i.e. assigning the unary + of b to a.
Examples:
int a = 15;
int b = -5;
a += b; // a is now 10
a =+ b; // a is now -5
+= is a compound assignment operator - it adds the RHS operand to the existing value of the LHS operand.
=+ is just the assignment operator followed by the unary + operator. It sets the value of the LHS operand to the value of the RHS operand:
int x = 10;
x += 10; // x = x + 10; i.e. x = 20
x =+ 5; // Equivalent to x = +5, so x = 5.
+= → Add the right side to the left
=+ → Don't use this. Set the left to the right side.
a += b equals a = a + b. a =+ b equals a = (+b).
x += y
is the same as
x = x + y
and
x =+ y
is wrong but could be interpreted as
x = 0 + y
It's simple.
x += 1 is the same as x = x + 1 while
x =+ 1 will make x have the value of (positive) one
Some historical perspective: Java inherited the += and similar operators from C. In very early versions of C (mid 1970s), the compound assignment operators had the "=" on the left, so
x =- 3;
was equivalent to
x = x - 3;
(except that x is only evaluated once).
This caused confusion, because
x=-1;
would decrement x rather than assigning the value -1 to it, so the syntax was changed (avoiding the horror of having to surround operators with blanks: x = -1;).
(I used -= and =- in the examples because early C didn't have the unary + operator.)
Fortunately, Java was invented long after C changed to the current syntax, so it never had this particular problem.
Because =+ is not a Java operator.