Yes, a cast is required.
If you don't cast at least one of the int
arguments to long
, the multiplication will be performed as a 32 bit operation. If multiplication overflows, the value ultimately assigned to result
will be incorrect.
As the JLS states (15.7.1):
If an integer multiplication overflows, then the result is the low-order bits of the mathematical product as represented in some sufficiently large two's-complement format. As a result, if overflow occurs, then the sign of the result may not be the same as the sign of the mathematical product of the two operand values.
If either of the operands is long
, the multiplication will be performed as a 64 bit operation, and the operation won't overflow.
(There are other ways to cause the operation to be performed using long
arithmetic, but a type cast is the most idiomatic.)