Java Reference
In-Depth Information
i | j - returns the value of a bitwise OR operation on bits in the two variables.
4.0 * Math.sin (i * Math.PI) - combines several operations in this ex-
pression, including multiplication and a method call.
Expressions involve at least one operator. A value is returned from the expression
and, in some cases, an operand is also modified (such as ++k ).
2.8 Operators
In Java an expression carries out some operation or operations that act according
to the particular operator in use. An operator acts upon one, two, or three operands.
Here we discuss some general properties of operators and their operands. Refer
to Appendix 2 for tables of the allowed operators in Java.
2.8.1 Operands
An operand can be:
a numeric variable - integer, floating-point, or character
any primitive type variable - numeric and boolean
a reference variable to an object
a literal numeric value, boolean value, or string
an array element - a[2]
char primitive, which in a numeric operation is treated as an unsigned 2-byte integer.
The operator is unary if it acts on a single operand; binary if it requires two
operands. The conditional operator, to be discussed later, is the only ternary
operator in Java.
Each operator places specific requirements on the operand types allowed.
Forexample, the subtraction operator - in x = a-b; requires that a and b
variables be numeric types. The assignment operator “= in that same expression
requires that x also be a numeric type. (If a and b were wider types than x ,a
casting operation, see Section 2.10, would also be required.)
2.8.2 Returned value
Avalue is “returned” at the completion of an operation. The following statements
use the assignment operator “= and the addition operator + :
int x = 3;
int y = x+5;
These statements result in x holding the value 3 and y holding the value 8. The
entire expression y = x+5 could be used in another expression:
int x = 3;
int y;
int z = (y = x+5)*4;
Search WWH ::




Custom Search