What does the "+=" operator do in Java?
Asked Answered
O

8

57

Can you please help me understand what the following code means:

x += 0.1;
Obryant answered 17/9, 2011 at 17:10 Comment(2)
Looks like adding 0.1 to a variable named x..Hildy
Almost a duplicate of #609221Vomiturition
C
143

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].

Crave answered 17/9, 2011 at 17:25 Comment(5)
Additionally, if x is a more complicated expression (like array or field access) instead of a single variable, its component expressions are now only evaluated once instead of twice.Checklist
X – = Y + 1 means X- =(Y+1) means X= X-(Y+1)=X-Y-1, Correct? But, somewhere, it gives X – = Y + 1 means X= X- Y+1, it this, which one is correct? @CraveUnpleasantness
@MithleshUpadhyay The first. Where did you get the second idea from?Bullen
This answer is a bit hard to follow. It emphasizes that x += y is not identical to x = x + y, while it mostly is. Only in some rare use-cases it's not (and even then, it's mostly the same).Discommend
@Discommend you're right. Originally I wrote this answer long time ago, among dozens of "it's identical to x = x + y". I reworded the answer to make it more digestible.Crave
I
29
  • 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 ...

Impeditive answered 17/9, 2011 at 17:24 Comment(3)
how about =+ ? it compiles but does nothingBirdt
@Birdt x =+ y is equivalent to x = y.Impeditive
@Birdt x =+ y is x = 0 + yTurnstone
U
13

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.

Urinary answered 17/9, 2011 at 17:12 Comment(0)
H
3

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
Hargett answered 31/8, 2009 at 2:34 Comment(0)
R
2

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;

Relax answered 29/6, 2018 at 19:51 Comment(0)
R
2

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 :)

Relax answered 20/9, 2019 at 12:40 Comment(0)
F
1

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

Furl answered 31/8, 2009 at 2:36 Comment(0)
O
0

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;
Obed answered 19/11, 2019 at 19:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.