The error is more complicated than you originally thought, because it is actually the "+" operator, which causes "a possible loss of error accuracy." The error can be resolved if the move is performed:
s[i] = (char)('A' + (num[i]- 1));
<b> Explanation
The first bullet list of §5.6.2 Binary numeric promotion in the Java Language Specification states that:
When an operator applies binary numeric promotion to a pair of operands [...], the following rules apply in order to use the advanced conversion (§5.1.2) to convert operands as necessary:
- If any of the operands has a reference type, the decompression conversion is performed (Section 5.1.8). Then:
- If one of the operands is of type double, the other is converted to double.
- Otherwise, if either operand is of type float, the other is converted to float.
- Otherwise, if either operand is of type long, the other is converted to long.
- Otherwise, both operands are converted to type int.
The following bullet list indicates that:
Binary numeric promotion is performed on the operands of certain operators:
- Multiplicative operators *, / and% (§15.17)
- Addition and subtraction operators for numeric types + and - (§15.18.2)
- Numerical comparison operators and> = (§15.20.1)
- Numerical equality operators == and! = (§15.21.1)
- Integer bitwise operators &, ^ and | (§15.22.1)
- In some cases, the conditional operator ?: (§15.25)
In your case, this means:
s[i] = (int)'A' + (int)((char)(num[i] - (int)1));
hence the error.
matsev
source share