The difference in results is related to the priority of the operator. In Java, multiplication and division have the same priority and therefore are evaluated from left to right.
So your first example is equivalent
double result = (4/3)*Math.PI*Math.pow(r,3);
Here 4/3 is a division of two integers, and in Java in such cases an integer division is performed with the result 1. To solve this problem, you must explicitly make sure that one or both of the operands are double:
double result = (4.0/3.0)*Math.PI*Math.pow(r,3);
The second example, however, is equivalent
double result = (Math.PI*Math.pow(r,3)*4)/3;
Here the part of Math.PI*Math.pow(r,3)*4 is evaluated as double, and therefore we no longer have integer division, and you get the correct result.
source share