Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The error is more complex than you would initially think, because it is actually the '+' operator that causes the "possible loss of precision error". The error can be resolved if the cast is moved: </p> <pre><code>s[i] = (char)('A' + (num[i]- 1));</code></pre> <p><br/> <b>Explanation</b> <br/> In the first bullet list of <a href="http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.6.2" rel="noreferrer">§5.6.2 Binary Numeric Promotion</a> in the <a href="http://java.sun.com/docs/books/jls/" rel="noreferrer">Java Language Specification</a> it is stated that: </p> <blockquote>When an operator applies binary numeric promotion to a pair of operands [...] the following rules apply, in order, using widening conversion (§5.1.2) to convert operands as necessary:<br/><br/> <ul><li>If any of the operands is of a reference type, unboxing conversion (§5.1.8) is performed. Then:</li> <li>If either operand is of type double, the other is converted to double.</li> <li>Otherwise, if either operand is of type float, the other is converted to float.</li> <li>Otherwise, if either operand is of type long, the other is converted to long.</li> <li><b>Otherwise, both operands are converted to type int.</b></li></ul></blockquote> <p>In the next bullet list it is stated that:</p> <blockquote>Binary numeric promotion is performed on the operands of certain operators:<br/><br/> <ul><li>The multiplicative operators *, / and % (§15.17)</li> <li><b>The addition and subtraction operators for numeric types + and - (§15.18.2)</b></li> <li>The numerical comparison operators , and >= (§15.20.1)</li> <li>The numerical equality operators == and != (§15.21.1)</li> <li>The integer bitwise operators &, ^, and | (§15.22.1)</li> <li>In certain cases, the conditional operator ? : (§15.25)</li></ul></blockquote> <p>In your case, that translates to: <pre><code>s[i] = (int)'A' + (int)((char)(num[i] - (int)1));</code></pre>hence the error.<br/> </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload