Java Reference
In-Depth Information
The increment and decrement operation can be specified before or after the variable. For instance, if an
increment is specified before the variable (++intTest), it is called a pre-increment. If the increment is specified
after the variable (intTest++), it is called a post-increment. Whether the operation is a pre or post does not make a
difference when the operation is specified by itself (as in the above examples). However, if the operation is embedded
in another statement the results may differ.
For instance, pre operations are done before the “owning” operation is performed. In the following statement:
System.out.println(++intTest);
the pre-increment is embedded in the println operation and the println operation is considered the “owning”
operation. Embedded operations are usually performed first (and the pre-increment is not an exception), then the
owning operation (the println) is performed. So assuming that intTest is equal to 2, first the value of intTest is changed
to 3, and then the value of intTest is displayed in the console as 3.
In the following println statement, we imbedded a post-increment:
System.out.println(intTest++);
Post-operations are performed after the owning operation. In this case, it means that first the println function
is performed and then the post-increment. Again, assuming that intTest is equal to 2, the above statement will first
display the value of intTest as 2 in the console and then the value of intTest will be incremented to 3.
Yikes! Isn't math fun? The only words of advice I can offer are, “If you don't like pre and post operations, don't use
them.” (Nevertheless, you should be able to read them in case another programmer does use them.)
Finally, when combining mathematical operations, the standard order of precedence is followed:
Multiplication and division first, from left to right
Then addition and subtraction, from left to right
So, the following statement would result in the value 16:
System.out.println(4.0+3.0/2.0*8.0);
Are you asking, “How the heck does that equal 16?”. If you think like the machine and follow the rules, the above
formula is equivalent to 4.0 + (( 3.0 / 2.0 ) * 8.0 ) . Why? Because multiplication and division are done first, from
left to right. So the addition operation is ignored even though it is the first operation in the formula. The JVM goes
from left to right and runs into the division (3.0 / 2.0 ) first. The division is performed, resulting in 1.5. As far as the
JVM is concerned, the formula now looks like the following: 4.0 + 1.5 * 8.0. So what happens next? That's right the
multiplication (1.5 * 8.0) is performed, resulting in 12. Now the JVM thinks the formula is 4.0 + 12.0. Since the addition
is the only operation left, the JVM performs the addition, and the result is displayed as 16 in the console.
You can control the order in which operations are performed with parentheses. Anything enclosed in
parentheses is done first, and then the normal order of precedence is followed. For instance, changing the formula to
the following:
System.out.println((4.0+3.0)/(2.0*8.0));
results in the value 0.4375 being displayed. Why? Because the addition (in the leftmost parentheses) is done first,
resulting in 7. Next the multiplication is done (because it is also in parentheses), resulting in 16. That leaves 7 divided
by 16, which equals 0.4375.
 
Search WWH ::




Custom Search