Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>These are the relevant JLS sections:</p> <h3><a href="http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.1.1" rel="nofollow noreferrer">JLS 5.1.1 Identity Conversion</a></h3> <blockquote> <p>A conversion from a type to that same type is permitted for any type. </p> </blockquote> <h3><a href="http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.2" rel="nofollow noreferrer">JLS 5.2 Assignment Conversion</a></h3> <blockquote> <p>Assignment conversion occurs when the value of an expression is assigned to a variable: the type of the expression must be converted to the type of the variable. Assignment contexts allow the use of one of the following:</p> <ul> <li>Identity conversion</li> <li>[...]</li> </ul> <p>In addition, if the expression is a constant expression of type <code>byte</code>, <code>short</code>, <code>char</code> or <code>int</code> :</p> <ul> <li>A narrowing primitive conversion may be used if the type of the variable is <code>byte</code>, <code>short</code>, or <code>char</code>, and the value of the constant expression is representable in the type of the variable.</li> </ul> </blockquote> <p>The above rules explain all of the following:</p> <pre><code>short a = 4; // representable constant short b = 5; // representable constant short c = 5 + 4; // representable constant short d = a; // identity conversion short e = a + b; // DOES NOT COMPILE! Result of addition is int short z = 32767; // representable constant short z_ = 32768; // DOES NOT COMPILE! Unrepresentable constant </code></pre> <p>As to why this doesn't compile:</p> <pre><code>test(7); // DOES NOT COMPILE! There's no test(int) method! </code></pre> <p>It's because the narrowing conversion with constant is only defined for assignments; not for method invocation, which has entirely different rules.</p> <h3><a href="http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.3" rel="nofollow noreferrer">JLS 5.3. Method Invocation Conversion</a></h3> <blockquote> <p>Method invocation conversions specifically do not include the implicit narrowing of integer constants which is part of assignment conversion. The designers of the Java programming language felt that including these implicit narrowing conversions would add additional complexity to the overloaded method matching resolution process.</p> </blockquote> <p>Instead of explaining how method resolution works precisely, I will just quote <em>Effective Java 2nd Edition</em>, Item 41: Use overloading judiciously:</p> <blockquote> <p>The rules that determine which overloading is selected are extremely complex. They take up <em>thirty-three</em> pages in the language specification, and few programmers understand all of their subtleties.</p> </blockquote> <hr> <h3>See also</h3> <ul> <li><a href="https://stackoverflow.com/questions/2696812/varying-behavior-for-possible-loss-of-precision/2696819#2696819">Varying behavior for possible loss of precision</a> <ul> <li><code>short x = 3; x += 4.6;</code> compiles because of semantics of compound assignment</li> </ul></li> </ul>
 

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