The other day, I ran into some weird behavior while using the compound multiplication operator, *=. Well actually, I only thought I did, but it got me researching these operators and I got the idea that I’d like to write about some of their gotchas. It was the holidays, so I didn’t have time to act on it and eventually I decided that I should write a Java Basics post about their general use first. So here we are.
Java provides a standard set of arithmetic operators for… wait for it… doing arithmetic. They’re all recognizable symbols from your basic math classes except maybe the modulus operator (%) which gives you the remainder part from a division problem.
The arithmetic operators can be used the way you would expect:
int x = 1 + 2;
Are you wondering why you’d use the modulus operator? It’s a good way to find out if a number is divisible by another number. So if you want to know if something is an even number you can use the remainder division (e.g. 20 % 2 = 0) and if the result is zero, it’s an even number.
Compound Arithmetic Operators
Many (most?) programming languages also provide for a shorthand way of operating on a variable and assigning the result back to itself. Java is no exception. We can do compound arithmetic operations with all of the arithmetic operators.
Assuming we’ve defined x:
int x = 5;
x = x + 2;
is equivalent to
x += 2;
Let’s look at a functioning example showing all five operators in use:
int x = 5; System.out.println("x = " + x); x += 6; System.out.println("After x += 6, x = " + x); x -= 7; System.out.println("After x -= 7, x = " + x); x *= 10; System.out.println("After x *= 10, x = " + x); x /= 10; System.out.println("After x /= 5, x = " + x); x %= 2; System.out.println("After x %= 2, x = " + x);
Should give us the following output:
x = 5 After x += 6, x = 11 After x -= 7, x = 4 After x *= 10, x = 40 After x /= 5, x = 4 After x %= 2, x = 0
So that’s how to use the compound arithmetic operators. There are some gotchas that hopefully I’ll cover in a separate article.
Java also provides us with another arithmetic shorthand trick with it’s unary operators. We can use these for incrementing and decrementing a variable by one. An extremely common use-case is loop counters.
To add one to a variable, we use ++ and for subtracting one we use —. The operator can go before or after the variable, but the positioning matters. If placed before the variable, the addition or subtraction happens before the statement is evaluated. When the operator is after the variable, but statement is evaluated and then the variable is incremented/decremented.
That’s a little confusing to try to write out, so let’s just look at an example:
int i = 0; System.out.println("i++ = " + i++ + ", final i = " + i); i = 0; System.out.println("++i = " + ++i + ", final i = " + i); i = 5; System.out.println("i-- = " + i-- + ", final i = " + i); i = 5; System.out.println("--i = " + --i + ", final i = " + i);
Look closely at the println statements and the output to see the difference in positioning:
i++ = 0, final i = 1 ++i = 1, final i = 1 i-- = 5, final i = 4 --i = 4, final i = 4
As you can see, the initial value of i is different depending on where the operator is.
This quick post should provide a basic overview of how compound and unary arithmetic operators are used in Java.
The example code is over on BitBucket.