In the Appending Strings tutorial, we identified that we could append one string to an existing string variable using the
+= operator. We can use the same operator, and other similar ones, to perform mathematic operations on numeric values. Consider the following code:
This could be re-written as
int total = 2; total = total + 5; //total is 7
int total = 2; total += 5 //total is 7
This process is known as addition assignment, and the technique can be used with other mathematic operators, in each case the process is similar, for example:
is the same as
int someValue = 7; someValue = someValue % 3; //someValue is now 1
int someValue = 7; someValue %= 3; //someValue is now 1
It is quite common in programming to need to add or remove 1 from an integer value, so an even shorter version of addition assignment exists for adding 1 to a value - all that is required is to append
++ to the variable name. This is known as the increment operator. Similarly to subtract 1 from a variable
-- is appended (this is known as the decrement operator).
The following example shows the three ways of adding one to a value, and then an example of how to use the decrement operator:
To decrease a value by one, you simply use
int counter = 0; counter = counter + 1; //counter is 1 counter += 1; //counter is 2 counter++; //counter is 3 counter--; //counter is 2
The Java Documentation contains a full list of Java operators
Sometimes calculations performed might be complex, with a number of operators being used at once. Depending on which part of the calculation is done first, the result of the calculation may vary. Java has a preset order in which calculations are completed.
Multiplication, division and modulo operations will be performed first, followed by addition and subtraction. In the example below, both calculations will have the same result as 6 is multiplied by 7 (to make 42), before 8 is added on:
If there are more than one (for example a multiplication and division in the same calculation), then the left most operation will be performed first, as per the example here:
int a = 6 * 7 + 8; //a is 50 int b = 8 + 7 * 6; //b is 50
int c = 8 / 3 * 2; //result is 4 int d = 2 * 8 / 3; //result is 5
If the calculation includes assignment, then the assignment will be performed with the rightmost first, but after any addition / subtraction / multiplication / division or modulo calculation
If you wish to force a certain part of the calculation to be performed first, then it can be placed in brackets - consider the following example:
In this, the 4 would be multiplied by 3, (to make 12) then two added - so the result is 14, contrast that with this:
int x = 4 * 3 + 2; //x is 14
whereby 3 will be added to 2 (to make 5) which and then multiplied by 4, so the result is 20.
int y = 4 * (3 + 2); //y is 20
There is a comprehensive list of operator precedence available in the Java Documentation
For each of the following calculations, determine what the result would be (without writing the code). Once you have determined all the answers, check them by putting the formulas into a programme one at a time and outputting the results.
int a = 1 + 2 - 3 / 4 * 5; int b = 1 - 2 / 3 * 4 + 5; int c = 1 / 2 * 3 + 4 - 5; int d = 1 * 2 + 3 - 4 / 5; int e = 1 * (2 + 3) - 4 / 5; int f = (1 * (2 + 3) -4 ) / 5; int g = 3 / 2 - 2; int h = 3 / (2 - 2);
It is expected that neither you, nor the computer, will be able to solve one of the problems, try and understand why this is.