Java Reference
In-Depth Information
Java has the arithmetic relations
<
,
<=
,
>
, and
>=
on values of type
int
. They
yield values of type
boolean
. For example,
1<2
evaluates to
true
and
2<=1
evaluates to
false
.
The operators
==
and
!=
denote equality and inequality:
1==2
is
false
and
1!=2
is
true
.
Precedence and associativity
One can always fully parenthesize expressions to make absolutely clear in
which order the operators are to be evaluated. For example, in the fully paren-
thesized expression
(5 + 5) * ((5 / 5) % 6)
, the order of evaluation is: the addi-
tion, the division, the remainder, and the multiplication.
However, writing so many parentheses can be a pain. To reduce the number
of parentheses required in many expressions, mathematical conventions assign
precedences to operators, which indicate the order of evaluation. For example,
negation has precedence over
*
and
*
has precedence over
+
. Thus, in the expres-
sion
-10+4*2
, first the negation is performed, then the multiplication, and
finally the addition. If two operators with different precedences appear next to
each other, the one with the higher precedence is evaluated first.
The precedences of all
int
operators are as follows, with the highest first:
1.
negation, or unary minus.
2.
*
and
/
and
%
, with the same precedence.
3.
+
and
-
, with the same precedence.
If two operators with the same precedence appear next to each other, the
associativity
of the operators determines which is evaluated first:
• Unary
+
and unary
-
are
right associative
, which means that they are
evaluated right to left. For example,
---5
is equivalent to
-(-(-5)))
.
• The binary operators
+
,
-
,
*
,
/
, and
%
are
left associative
, which means
that they are evaluated left to right. For example, the expression
5-6-
3
is an abbreviation for the expression
(5 - 6) - 3
. This is different from
5-(6-3)
. So you really have to know whether an operator is left asso-
ciative or right associative when evaluating (or writing) an expression.
6.2
Types
byte
,
short
,
and long
6.2.1
Types byte and short
The values of type
byte
and
short
are the integers in these ranges:
byte
: -128..127, or -2
7
..2
7
-1
.
short:
-32768..32757, or -2
15
..2
15
-1
.
A value of type
byte
occupies one byte; a value of type
short
, two bytes.
The following constants give the minimum and maximum values of these
types:
Search WWH ::
Custom Search