If the product of 2 int
values does not fit in an int
, and thus I store it in a long
, do I need to specify explicit cast to long
before each operand (or at least before one of the operands)? Or does the compiler correctly handle it even if there is no cast?
This would be the explicit code:
public final int baseDistance = (GameCenter.BLOCKSIZE * 3/2);
long baseDistanceSquare = (long)baseDistance * (long)baseDistance;
Or is the below code sufficient?
long baseDistanceSquare = baseDistance * baseDistance;
As a side note, this is equivalent to the problem of converting to float the result of an operation with integers; for example:
float f = 2/3;
System.out.println(f); // Print 0.0
f = (float)(2/3);
System.out.println(f); // Print 0.0
f = (float)2/3;
System.out.println(f); // Print 0.6666667