Java Reference
InDepth Information
Expression evaluation.
Appendix I shows you how to evaluate expressions in a number of Java
platforms. Currently, for the purpose of evaluating expressions, the IDEs
DrJava and BlueJ offer the best support, although others plan to add support
this year. DrJava and BlueJ are free, and they are easy to download and install.
Whatever IDE your course is using, you can use one of these IDEs on your
own in order to learn.
1.1.
Types and expressions
In this section, we introduce you to basic Java expressions. Here are examples:
Tip:
Use les
son 6 as a ref
erence for
expressions
and the types
discussed in
this section.
25
3
(1+2+3+4+5+6+7+8+9+10)*2
2147483647 + 1
These expressions are like mathematical expressions that you have seen and
used before. You can use integer constants like
42
, called integer
literals
in Java,
and you can use the operations defined in Fig. 1.1. Also, you can use parenthe
ses to help indicate the order in which operations are to be performed, as in
(3 +
4) * 2
. If you do not use parentheses, operations are carried out using conven
tional mathematical
precedences
:
•
Unary

and
+
have highest precedence, so
5 + 6
has the value
1
.
•
Multiplication, division, and remainder come next, and sequences of
them are carried out from left to right. So,
4+3*6/2*3
is
4+18/2
*3
, which is
4+9*3
, which is
4+27
, which is
31
.
•
Addition and subtraction have lowest precedence, and sequences of them
are carried out from left to right. So
434*2
is
438
, which is
1
8
, which is
7
.
Operator
Operation
Example
Result
unary
+
no effect
+6
6
binary
+
addition
5+6
11
negation
unary

9
(4 + 5)
binary

subtraction
1
45
multiplication
*
4*5
20
division
/
8/2
4
remainder
1
(
13/3 = 4
with remainder
1
)
%
13 % 3
Figure 1.1:
Basic integer operators in Java
Search WWH ::
Custom Search