tl;drJUST DO WHAT ISAI said, kthx.
Addition
The + operator performs an addition of two values. This can be an addition of two constants, a constant and a variable, or a variable and a variable. Here are a few Java addition examples:
Code:
int sum1 = 10 + 20; // adding two constant values
int sum2 = sum1 + 33; // adding a variable and a constant
int sum3 = sum2 + sum2; // adding two variables
The + operator will replace the two values with the sum of the two values at runtime. So at the place where the expression sum1 + 33 is written, at runtime this will be replaced with the sum of the value of sum1 and the constant value 33.
You can of course create longer chains of additions and thus add more than two numbers. Here is a en example of adding 3 or more values together:
Code:
int sum1 = 10 + 20 + 30;
int sum3 = sum2 + 99 + 123 + 1191;
A commonly used math operation on variables is to set the variable equal to its own value plus another value. Here is how that looks:
Code:
int result = 10;
result = result + 20;
The second line of this example sets the sum variable equals to its own value (before being assigned the new value) + 20. Which means 10 + 20.
Since adding a value to a variable is a very common operation, Java contains a built-in operator for that specific purpose. It is the += operator. Here is the example above rewritten to use the
+= operator:
Code:
int result = 10;
result += 20;
The second line in this example adds 20 to the variable result. This equivalent to the code result = result + 20;