Yes, this is always true.
It is a property that holds because you are effectively doing arithmetic modulo 2^32. The fact that Java int
s are signed complicates things slightly (and means that you can't assume you are doing the equivalent of modulo arithmetic in general), but doesn't affect this particular distributive property.
A thought experiment is to consider implementing it using repeated addition, and consider what happens when it overflows. Since the order of doing additions doesn't affect the result with int
s (even with overflows) then neither does doing the multiplications as repeated additions in a different order. And since int
multiply is always equivalent to repeated addition, the results must also be the same for re-ordered multiplication. Q.E.D.