Java Reference
In-Depth Information
When two operators of the same precedence appear in a statement, the
Java language specifies that they be evaluated from left to right. For
example, in the following statement, the addition and subtraction
operators have the same precedence and will be evaluated left to right:
int x = 5 + 4 - 3;
The 5 + 4 will be evaluated, and then the 3 will be subtracted from 9 to
get 6.
Assignment Operators
Java provides a collection of shortcut assignment operators that are based on a
similar feature of C++. A variable can be used in a statement and assigned to
the result, all in one statement.
For example, the following statement uses the multiplication with assign-
ment operator:
d *= 4.0;
In the preceding statement, d is multiplied by 4.0, and the result is stored in
d. The statement is equivalent to the following:
d = d * 4.0;
Table 2.4 contains a list of all the operators with assignment. This is another
one of those features of Java that you do not use every day, but I want you to
at least be familiar with it because you may run into it when reading other
developers' Java code.
Shift Operators
There are three shift operators in Java: one left-shift operator (<<) and two
right-shift operators (>> and >>>). Shift operators act on integer values by
shifting their binary values (how they are stored in memory) to the right or
left.
Shifting an integer to the left causes a 0 to be placed in the least-significant
digit, shifting all the other 1s and 0s one place to the right, and having the most
significant digit “pushed” off the end and lost. For example, the following is 45
represented in binary format:
0 0 1 0 1 1 0 1
Search WWH ::




Custom Search