## What does the "+=" operator do in Java?

### Question

``````x += 0.1;
``````
2011/09/17
1
43
9/17/2011 5:16:37 PM

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 `int`s), 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].

2017/12/10
117
12/10/2017 12:42:43 PM

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.

2011/09/17

`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
``````
2017/12/09

`devtop += Math.pow(x[i] - mean, 2);` adds `Math.pow(x[i] - mean, 2)` to `devtop`.

2017/12/09

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;

2018/06/29

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;
``````
2020/05/08