Grundrechnen für Anfänger

exercise No. 262

F:

We consider:

Code
System.out.println("Result 1: " + 2.5 * 50 / 100);
System.out.println("Result 2: " + 50 / 100 * 2.5);
Result
Result 1: 1.25
Result 2: 0.0

Explain these two different outcomes despite just re-ordering factors in a product.

A:

Both operators * und / are being evaluated from left to right. In case of integer division this yields a zero net result:

2.5 * 50 / 100
2.5 * 50 / 100
  ↘  ↙     ↙
 125.0   ↙
    ↘  ↙
    1.25
50 / 100 * 2.5
50 / 100 * 2.5
  ↘  ↙     ↙
    0    ↙
    ↘  ↙
      0