The operators that are used in Java are very similar to those used in C or C++. We can classify operators into five basic types: arithmetic, increment/decrement, logical, comparison, and assignment.
The order of precedence on these operators is as follows:
Increment or decrement
Arithmetic
Comparisons
Logical
Assignment
As with most other languages, parts of an expression can be given higher precedence by using parentheses.
Similar to the C programming language, Java allows you to increment and decrement variables using the increment (++) and decrement (--) operators either before or after the variable name.
int x = 5; // initialize ‘x' to the value ‘5' x++; // after this line, ‘x' is equal to ‘6'
Whether the operators are placed before or after the variable can make quite a difference in how the code functions. If the operator is placed before the variable, the increment or decrement happens before any other operation in the statement. If the operator is placed after the variable, the increment or decrement happens after all other operations in the statement.
int x1 = 5, y1 = 3, z1; int x2 = 5, y2 = 3, z2; /* Case # 1 */ z1 = ++x1 * y1; // After this statement z1=18 & x1=6 /* Case # 2 */ z2 = x2++ * y2; // After this statement z2=15 & x2=6
Table 7.2 shows the basic arithmetic operators that can be applied to all numeric data types.
Table 7.2 Java Arithmetic Operators
Operator | Use | Description |
---|---|---|
+ | x + y | Addition |
- | x - y | Subtraction |
* | x * y | Multiplication |
/ | x / y | Division |
% | x % y | Modulus |
Note | The addition operator (+) can also be used for string concatenation. Strings and arrays are discussed in more detail in the 'Strings and Arrays' section later in this chapter. |
Comparison operators are used to perform some type of test on two variables and then return a simple true or false Boolean result. The comparison operators for Java are listed in Table 7.3.
Table 7.3 Java Comparison Operators
Operator | Use | Returns true If |
---|---|---|
> | x > y | x is greater than y |
< | x < y | x is less than y |
>= | x >= y | x is greater than or equal to y |
<= | x <= y | x is less than or equal to y |
== | x == y | x is equal to y |
!= | x != y | x is not equal to y |
The comparison operators in Table 7.3 can be combined to form a more complex expression through the use of logical operators. Table 7.4 summarizes the logical operators.
Table 7.4 Java Logical Operators
Operator | Use | Returns true If |
---|---|---|
&& | exp1 && exp2 | Both expressions are true |
|| | exp1 || exp2 | At least one of the expressions is true |
! | ! exp1 | The expression is false |
^ | exp1 ^ exp2 | One expression is true and the other is false |
~ | ~ exp1 | Bitwise complement |
There is one catch with the && and || operators. You can use & instead of && and | instead of ||; these elements are bitwise operators. In these cases, Java evaluates the second expression only if necessary. This approach can save on execution time if you have very complicated expressions, but can also lead to unexpected results if you combine other operators in your expressions, as shown here:
int x = 5, y = 4, z = 3; boolean testResult1 = ( x < 4 ) & ( y > ++z); boolean testResult2 = ( X < 4 ) && (y > ++z);
In the preceding code sample, both testResult1 and testResult2 return false. z is not incremented (because x < 4 is not true) in the first case, but it is in the second case.
Assignment operators are the tools that allow us to change the value of variables. Table 7.5 summarizes the Java assignment operators.
Table 7.5 Java Assignment Operators
Operator | Use | Results |
---|---|---|
+= | x += y | x = x + y |
-= | x -= y | x = x - y |
*= | x *= y | x = x * y |
/= | x /= y | x = x / y |
%= | x %= y | x = x % y |
&= | x &= y | x = x & y |
|= | x |= y | x = x | y |
^= | x ^= y | x = x ^ y |
>> | x >> y | Shift bits of x right by y places |
<< | x << y | Shift bits of x left by y places |
>>> | x >>> y | Shift bits of x right by y places (unsigned) |
Team-Fly |