Java Reference
In-Depth Information
White space
Note the use of white space in Fig. 2.15. Recall that the compiler normally ignores white
space. So, statements may be split over several lines and may be spaced according to your
preferences without affecting a program's meaning. It's incorrect to split identifiers and
strings. Ideally, statements should be kept small, but this is not always possible.
Error-Prevention Tip 2.5
A lengthy statement can be spread over several lines. If a single statement must be split
across lines, choose natural breaking points, such as after a comma in a comma-separated
list, or after an operator in a lengthy expression. If a statement is split across two or more
lines, indent all subsequent lines until the end of the statement.
Operators Discussed So Far
Figure 2.16 shows the operators discussed so far in decreasing order of precedence. All but
the assignment operator, = , associate from left to right . The assignment operator, = , associ-
ates from right to left . An assignment expression's value is whatever was assigned to the vari-
able on the = operator's left side—for example, the value of the expression x = 7 is 7 . So an
expression like x = y = 0 is evaluated as if it had been written as x = (y = 0) , which first
assigns the value 0 to variable y , then assigns the result of that assignment, 0 , to x .
Operators
Associativity
Type
* / %
left to right
multiplicative
+ -
left to right
additive
< <= > >=
left to right
relational
== !=
left to right
equality
=
right to left
assignment
Fig. 2.16 | Precedence and associativity of operators discussed.
Good Programming Practice 2.12
When writing expressions containing many operators, refer to the operator precedence
chart (Appendix A). Confirm that the operations in the expression are performed in the
order you expect. If, in a complex expression, you're uncertain about the order of evalua-
tion, use parentheses to force the order, exactly as you'd do in algebraic expressions.
2.9 Wrap-Up
In this chapter, you learned many important features of Java, including displaying data on
the screen in a Command Prompt , inputting data from the keyboard, performing calcula-
tions and making decisions. The applications presented here introduced you to many basic
programming concepts. As you'll see in Chapter 3, Java applications typically contain just
a few lines of code in method main —these statements normally create the objects that per-
form the work of the application. In Chapter 3, you'll learn how to implement your own
classes and use objects of those classes in applications.
 
 
Search WWH ::




Custom Search