Java Reference
In-Depth Information
1.4.2 binary arithmetic operators
Line 20 in Figure 1.3 illustrates one of the binary arithmetic operators that
are typical of all programming languages: the addition operator ( + ). The +
operator causes the values of b and c to be added together; b and c remain
unchanged. The resulting value is assigned to a . Other arithmetic operators
typically used in Java are - , * , / , and % , which are used, respectively, for sub-
traction, multiplication, division, and remainder. Integer division returns only
the integral part and discards any remainder.
As is typical, addition and subtraction have the same precedence, and this
precedence is lower than the precedence of the group consisting of the multi-
plication, division, and mod operators; thus 1+2*3 evaluates to 7. All of these
operators associate from left to right (so 3-2-2 evaluates to -1). All operators
have precedence and associativity. The complete table of operators is in
Appendix A.
Java provides sev-
eral binary arith-
metic operators ,
including + , - , * ,
/ , and % .
1.4.3 unary operators
In addition to binary arithmetic operators, which require two operands, Java
provides unary operators , which require only one operand. The most familiar
of these is the unary minus, which evaluates to the negative of its operand.
Thus -x returns the negative of x .
Several unary oper-
ators are defined,
including - .
Java also provides the autoincrement operator to add 1 to a variable—
denoted by ++ —and the autodecrement operator to subtract 1 from a variable—
denoted by -- . The most benign use of this feature is shown on lines 22 and
23 of Figure 1.3. In both lines, the autoincrement operator ++ adds 1 to the
value of the variable. In Java, however, an operator applied to an expression
yields an expression that has a value. Although it is guaranteed that the vari-
able will be incremented before the execution of the next statement, the
question arises: What is the value of the autoincrement expression if it is
used in a larger expression?
In this case, the placement of the ++ is crucial. The semantics of ++x is
that the value of the expression is the new value of x . This is called the prefix
increment . In contrast, x++ means the value of the expression is the original
value of x . This is called the postfix increment . This feature is shown in line 24
of Figure 1.3. a and b are both incremented by 1, and c is obtained by adding
the original value of a to the incremented value of b .
Autoincrement and
autodecrement add
1 and subtract 1,
respectively. The
operators for doing
this are ++ and -- .
There are two
forms of increment-
ing and decrement-
ing: prefix and
postfix.
The type conversion
operator is used to
generate a tempo-
rary entity of a new
type.
1.4.4 type conversions
The type conversion operator is used to generate a temporary entity of a new
type. Consider, for instance,
 
Search WWH ::




Custom Search