Operators
Java also provides several operators that you can use to manipulate your variables.
Mathematical Operators
As in all languages, Java allows you to perform math functions.
The following table outlines the available functions:
| Operator |
Description |
Example |
| + |
Performs addition |
int x = 1;
int y = 2;
int z = x + y // z = 3 |
| - |
Performs subtraction |
int x = 2;
int y = 1;
int z = x - y // z = 1 |
| * |
Performs multiplication |
int x = 2;
int y = 2;
int z = x * y // z = 4 |
| / |
Performs division |
int x = 4;
int y = 2;
int z = x / y // z = 2 |
| % |
Gets the remainder (modulus) |
int x = 6;
int y = 4;
int z = x % y // z = 2 |
One thing to remember about mathematics in Java is that since variables are typed, you should be careful to match types or expect automatic casting!
In other words, Java will return the result in a type that fits in the result regardless of what was used in the equation.
Assignment Operators
As was alluded to in the last section, variables utilize assignment operators.
However, if you are working with a numerical type, you have more assignment operators than simply "=".
The following table outlines the other available assignment operators:
| Operator |
Description |
| = |
Equality. Note, you can chain assignments such as:
x=y=x=0 |
| += |
Addition (x=1;x+=2; //x=3 |
| -= |
Subtraction |
| /= |
Division. Don't forget about automatic casting! If your division returns a decimal number, it will be chopped to fit its type. |
| *= |
Multiplication |
| %= |
Modulus |
| ^= |
Bitwise XOR |
| &= |
Bitwise AND |
| |= |
Bitwise OR |
| <<= |
Left Shift |
| >>= |
Right Shift |
| >>>= |
Zero Fill Right Shift |
Incrementation and Decrementation
Java also provides the standard increment and decrement operators with prefix and postfix notation.
Remember that the prefix operator modifies the variable before is it acted upon whereas the postfix operator acts afterwards.
This is best seen by example:
int x = 3;
int y = 3;
int sum1 = 2 * x++; // sum1 is 6 and x is 4
int sum2 = 2 * ++y // sum2 is 8 and y is 4
int sum3 = 2 * x--; // sum3 is 6 and x is 2
int sum4 = 2 * --y // sum4 is 4 and y is 2
Precedence
Finally, Java maintains the usual operator hierarchy.
The precedence table is shown below:
| Precedence |
Operator |
Associativity |
| 1 |
++, --, ~!, +, -, (unary), (type cast) |
R |
| 2 |
* / % |
L |
| 3 |
+, -, + (concatination) |
L |
| 4 |
>>>, >>, << |
L |
| 5 |
<, <=, >, >= |
L |
| 6 |
==, != |
L |
| 7 |
& |
L |
| 8 |
^ |
L |
| 9 |
| |
L |
| 10 |
&& |
L |
| 11 |
|| |
L |
| 12 |
?: |
R |
| 13 |
=, *=, /=, %=, +=, -=, <<=, >>=, >>>=, &=, ^=, |= |
R |
NEXT
Selena Sol contributes to the JavaBoutique's Introduction to Java. Selena curently works for Barclays Capital in London, one of the leading global investment banks in Europe and has worked as a software developer for the National Center for Human Genome research, Microline Software, Neuron Data, and Electric Eye in Singapore. Selena is perhaps best-known for creating the Public Domain Web Script Archive (Extropia) and writing several books on Web Programming (Perl, CGI, Java).
Email: selena@extropia.com
|