Why Different Multiplication Results with and without "L"?
In Java, the multiplication operation produces different results when using the "L" suffix for one of the operands.
Adding "L" for Correct Long Value
Adding "L" ensures that the result is a long value. When multiplying integers, the result is also an integer. If the result exceeds the range of an integer (2147483647 to -2147483648), it overflows and becomes negative.
By adding "L" to 365 in the expression, you explicitly specify that it's a long value. This ensures that the result of multiplying it with the other integers is a long value and is not truncated to the integer range.
Without "L": Incorrect Integer Result
When multiplying integers without "L," the result is an integer. If the result exceeds the integer range, it "wraps around" and becomes a different value.
For example, the multiplication 1000606024365 without adding "L" gives the incorrect result 1471228928. This is because the result 31536000000 overflows the integer range and becomes -1702967296 (the 2's complement representation of 31536000000).
Binary Representation
The binary representations of the two results show the difference:
When you don't add "L," the four most significant bits are truncated, resulting in the incorrect representation 01010111101100010010110000000000, which corresponds to the incorrect result.
Other Considerations
The above is the detailed content of Why does adding "L" to one operand in a Java multiplication change the result?. For more information, please follow other related articles on the PHP Chinese website!