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.
Types and expressions
In this section, we introduce you to basic Java expressions. Here are examples:
Tip: Use les-
son 6 as a ref-
and the types
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 4-3-4*2 is 4-3-8 , which is 1
-8 , which is -7 .
-(4 + 5)
1 ( 13/3 = 4 with remainder 1 )
13 % 3
Figure 1.1: Basic integer operators in Java