What does the "+=" operator do in Java?
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
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.
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 += yis
x = x + y
x -= yis
x = x - y
x *= yis
x = x * y
x /= yis
x = x / y
x %= yis
x = x % y
x ^= yis
x = x ^ y
x &= yis
x = x & y
x |= yis
x = x | y
and so on ...
Read more... Read less...
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
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
A more simple example:
int devtop = 2; devtop += 3; // devtop now equals 5
devtop += Math.pow(x[i] - mean, 2); adds
Math.pow(x[i] - mean, 2) to
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";