When using binary operators with mixed operand sizes, be aware that some of the narrower operands may be promoted to a wider type, to match the type of the other operand. For example in the expression 'a' == 42, the field 'a' will be promoted to an int before the comparison is carried out.
| Wiki Markup |
|---|
According to \[[JLS 05|AA. Java References#JLS 05]\]: |
If an integer operator other than a shift operator has at least one operand of type long, then the operation is carried out using 64-bit precision, and the result of the numerical operator is of type long. If the other operand is not long, it is first widened (§5.1.5) to type long by numeric promotion (§5.6). Otherwise, the operation is carried out using 32-bit precision, and the result of the numerical operator is of type int. If either operand is not an int, it is first widened to type int by numeric promotion.
| Wiki Markup |
|---|
The Java Language Specification \[[JLS 05|AA. Java References#JLS 05]\] section 5.6 "Numeric Promotions" describes numeric promotion as: |
...
Widening conversions resulting from integer promotions preserve the overall magnitude of the number. However, promotions in which the operands are converted from a numeric type such as an integer to a float or a long to a double, are particularly pernicious (see the INT33-J rule . Be careful while casting numeric types to wider floating-point types for more details regarding this issue). These implicit casts can lead to an undesirable loss in precision.
...
| Wiki Markup |
|---|
\[[JLS 05|AA. Java References#JLS 05]\] 4.2.2 "Integer Operations" and 5.6 "Numeric Promotions" |