Java Reference
In-Depth Information
Compare this with the following statement:
payment = dollars + quarters * QUARTER_VALUE +
dimes * DIME_VALUE
+ nickels * NICKEL_VALUE + pennies *
PENNY_VALUE;
The advantage is obvious. Reading dollars is a lot less trouble than reading d
and then figuring out that it must mean Ȓdollarsȓ.
In practical programming, descriptive variable names are particularly important
when programs are written by more than one person. It may be obvious to you that
d stands for dollars, but is it obvious to the person who needs to update your code
years later, long after you were promoted (or laid off)? For that matter, will you
remember yourself what d means when you look at the code six months from now?
145
146
4.3 Assignment, Increment, and Decrement
The = operator is called the assignment operator. On the left, you need a variable
name. The right-hand side can be a single value or an expression. The assignment
operator sets the variable to the given value. So far, that's straightforward. But now
let's look at a more interesting use of the assignment operator. Consider the statement
items = items + 1;
It means, ȓCompute the value of the expression items + 1 , and place the result
again into the variable items .ȓ (See Figure 1 .) The net effect of executing this
statement is to increment items by 1. For example, if items was 3 before
execution of the statement, it is set to 4 afterwards. (This statement would be useful if
the cash register kept track of the number of purchased items.)
The = sign does not mean that the left-hand side is equal to the right-hand side.
Instead, it is an instruction to copy the right-hand-side value into the left-hand-side
variable. You should not confuse this assignment operation with the = relation used in
algebra to denote equality. The assignment operator is an instruction to do something,
namely place a value into a variable. The mathematical equality states the fact that
two values are equal. Of course, in mathematics it would make no sense to write that i
= i + 1; no integer can equal itself plus 1.
Search WWH ::




Custom Search