This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
Hi, Here are the answers to ur queries: The first statement,
1 - 1 / 3 * 3
will be evaluated as follows: 1 - (1/3) * 3 = 1 - (0 * 3) = 1 - 0 = 1. This is because in integer arithmetic operations, the division operator (/) has highest precedence followed by the multiplication operator (*) followed by Addition and Subtraction. Hence //1 returns false.
The evaluation proceeds as 1 - (1.0f / 3.0f) * 3.0d. But here we have a floating point and a double value. So the floating point operation is performed first...then converted to double...1 - 0.33333d * 3.0d. But converting from floating point to double causes some loss of precision and hence the result is <> 0. Hence //3 returns false.
1 - 1.0d / 3.0d * 3.0d
returns true as all operations are with double values. Reasoning is much the same as at //2