Since adding and subtracting one is so common, there are operators just for that.
- ++ increments by one
- -- decrements by one
nis 8, then
nequal to 9, and
n--makes it 7.
Yeah, you can put the operator in front of the numeric variable or after it. The difference is that, when the operator is in front (pre-increment and pre-decrement), then the expression evaluates as the changed value. When the operator is in the back (post-increment and post-decrement), then the expression evaluates as the original value ... but the change happens after that evaluation. Uh, this is confusing. OK, say
nis 8 again.
(++n) + 50== 59, and
(n++) + 50== 58, and
nbecomes 9 here too
nin that context. To be honest, most of the time I end up using the post- versions.
I should mention that I used parentheses in that last item to avoid any worry about Java doing the wrong thing. If I write
x+++y, what does that mean? Is it
x+(++y)? I think it's the first one, but I'll just use parentheses and not worry about it.
Anyway, you never need to use these, because you can obviously just use
+ 1, but they are handy.
Now I'm going to shift (har har) to talking about bitwise operators. These are for when you want to fiddle and twiddle the very bits inside numeric values.
- << is for shifting bits left
- >> is for signed-shifting bits right
- >>> is for unsigned-shifting bits right
- & is for bitwise AND
- | is for bitwise OR
- ~ is for bitwise NOT (inversion)
- ^ is for bitwise XOR ... honestly, I forgot they even had this one until I was writing this
Bitwise operators aren't used very much. Back in the day clever C programmers would use << and >> to multiply and divide by powers of 2, because it translates to faster machine instructions. These days, though, that's too clever; let the compiler figure out how to make things fast. Use bitwise operators in those rare instances when you really need to work with bits.
I'm going to mention right now that the bitwise operators don't shortcut, but the logical ones I discussed last time do. I'll expand on that sometime in the future.
The last set of operators left to discuss are the assignment operators. These exist because this sort of thing is really common.
x = x + y;Instead of typing all of that out, you can use the corresponding assignment operator.
x += y;This is another case like the increment and decrement operators, where you don't need the operators but they can be handy in certain situations. Use them only if they don't make the code less readable. Anyway, the following operators have corresponding assignment operators.
- +, -, *, /, and % become +=, -=, *=, /=, and %=
- &, ^, |, <<, >>, and >>> become &=, ^=, |=, <<=, >>=, and >>>=
And now, here at the end, I must take back some of what I said before, because there is one more operator that I must mention, which is used all the time, and that operator is =.
x = 1Wait, that's not ... how is that ... huh?
Yeah, you use the assignment operator, plain old =, to make zillions of Java statements, but the expression created by it also evaluates to something. Its value is whatever is on the right side of the assignment. Consider this bit of code.
int x = 7; System.out.println (x = 9);This actually compiles and prints 9.
Having explained this to you, I need to urge you not to do this a lot. It's weird looking and creepy. It looks like I meant to write
x == 9and print out the result of a comparison. The only context where I think this use of the evaluated value of an assignment makes sense is in a compound assignment like this.
int x, y, z; x = y = z = 0;Hey! You made it! Nice work. You should feel pretty empowered now, because you've covered all the Java operators, even the weird ones, so you can bust out whatever you need to perform your calculations.