Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Why is there no error in the first line but in second?</p> </blockquote> <p>Because that's the way the language is defined. There's no <code>byte + byte</code> operator, which is why the third line fails - <em>both</em> operands are promoted to <code>int</code> automatically. From <a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.18.2" rel="nofollow">section 15.18.2 of the JLS</a>:</p> <blockquote> <p>The binary + operator performs addition when applied to two operands of numeric type, producing the sum of the operands.</p> <p>...</p> <p>Binary numeric promotion is performed on the operands (§5.6.2).</p> </blockquote> <p>Now <a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.6.2" rel="nofollow">binary numeric promotion</a> always ends up with a value of <code>int</code>, <code>long</code>, <code>float</code> or <code>double</code>... <code>int</code> in this case. So the operation is to add two <code>int</code> values together, and the result is an <code>int</code>.</p> <p>For <code>b++</code>, however, the type is still <code>byte</code>. It's a <a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.14.2" rel="nofollow">postfix increment expression (section 15.14.2)</a>:</p> <blockquote> <p>The type of the postfix increment expression is the type of the variable.</p> </blockquote> <p>On a related note, this would be okay:</p> <pre><code>b += 3; </code></pre> <p>That's a <a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.26.2" rel="nofollow">compound assignment (section 15.26.2)</a>:</p> <blockquote> <p>A compound assignment expression of the form <code>E1 op= E2</code> is equivalent to <code>E1 = (T) ((E1) op (E2))</code>, where <code>T</code> is the type of <code>E1</code>, except that <code>E1</code> is evaluated only once.</p> </blockquote> <p>Note the casting part, which is why it works.</p>
    singulars
    1. This table or related slice is empty.
    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.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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