Java Reference
In-Depth Information
doubleTest (2.0) is raised to the third (intTest) power, resulting in 8.0. 8.0 is multiplied by the
int
value 3, resulting in
24.0. Next 24.0 is divided by the
int
value 2. The 2 is promoted to a double (2.0), and the division result is 12.0.
The second formula's division (3/2) is in parentheses; therefore, it is performed first.
System.out.println(Math.pow(doubleTest, intTest) * (3/2));
These are both
int
values, so the result is an
int
value of 1. (Remember: the fractional result is truncated in
division between
int
values.) The Math.pow method is performed, raising doubleTest (2.0) to the third (intTest)
power resulting in 8.0. 8.0 is then multiplied by the
int
1, resulting in 8.0. Tricky, tricky, tricky! Result:
12.0
8.0
Now the following simply prints the initialized value of intTest (3).
System.out.println(intTest);
Next intTest (3) is divided by 2, the result (1) is printed, and then the intTest post-increment is performed,
increasing intTest to 4.
System.out.println(intTest++/2);
The third of this group simply prints the value of intTest (4).
System.out.println(intTest);
Next intTest is pre-incremented, resulting in intTest being set to 5. Then intTest (5) is divided by 2, and the result (2)
is printed.
System.out.println(++intTest/2);
Finally, the value of intTest (5) is printed.
System.out.println(intTest);
Result:
3
1
4
2
5
The next example has both a pre- and post-increment.
System.out.println(doubleTest++ + ++intTest);
The pre-increment is done first, setting intTest to 4. The addition of doubleTest and intTest is then performed,
resulting in the value 6.0. Next, println displays the result (6.0) and, lastly, doubleTest is incremented by 1, resulting in
a value of 3.0.