Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>The difference is static typing of the expressions at compile time:</strong></p> <h2>Summary</h2> <pre><code>E1: `(false ? 1.0f : null)` - arg 2 '1.0f' : type float, - arg 3 'null' : type null - therefore operator ?: : type Float (see explanation below) - therefore autobox arg2 - therefore autobox arg3 E2: `(false ? 1.0f : (false ? 1.0f : null))` - arg 2 '1.0f' : type float - arg 3 '(false ? 1.0f : null)' : type Float (this expr is same as E1) - therefore, outer operator ?: : type float (see explanation below) - therefore un-autobox arg3 </code></pre> <h2>Detailed Explanation:</h2> <p>Here's my understand from reading through <a href="http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.25" rel="nofollow noreferrer">the spec</a> and working backwards from the result you got. It comes down to the type of the third operand of the f2 <em>inner</em> conditional is null type while the type of the third operand of the f2 <em>outer</em> conditional is deemed to be Float.</p> <p><em>Note:</em> Its important to remember that the determination of type and the insertion of boxing/unboxing code is done at compile-time. Actual execution of boxing/unboxing code is done at run-time.</p> <pre><code>Float f1 = (false ? 1.0f : null); Float f2 = (false ? 1.0f : (false ? 1.0f : null)); </code></pre> <p><strong>The f1 conditional and the f2 inner conditional: <em>(false ? 1.0f : null)</em></strong></p> <p>The f1 conditional and the f2 inner conditional are identical: <em>(false ? 1.0f : null)</em>. The operand types in the f1 conditional and the f2 inner conditional are:</p> <pre><code>type of second operand = float type of third operand = null type (§4.1) </code></pre> <p>Most of the rules in <a href="http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.25" rel="nofollow noreferrer">§15.25</a> are passed up and this final evaluation is indeed applied:</p> <blockquote> <p>Otherwise, the second and third operands are of types S1 and S2 respectively. Let T1 be the type that results from applying boxing conversion to S1, and let T2 be the type that results from applying boxing conversion to S2. The type of the conditional expression is the result of applying capture conversion (<a href="http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.1.10" rel="nofollow noreferrer">§5.1.10</a>) to lub(T1, T2) (<a href="http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.12.2.7" rel="nofollow noreferrer">§15.12.2.7</a>).</p> </blockquote> <pre><code>S1 = float S2 = null type T1 = Float T2 = null type type of the f1 and f2 inner conditional expressions = Float </code></pre> <p>Since for f1, the assignment is to a Float reference variable, the result of the expression (null) is successfully assigned.</p> <p><strong>For f2 outer conditional: <em>(false ? 1.0f : [f2 inner conditional])</em></strong></p> <p>For the f2 outer conditional, the types are:</p> <pre><code>type of second operand = float type of third operand = Float </code></pre> <p>Note the difference in operand types compared to the f1/f2 inner conditionals that reference the <em>null</em> literal directly (<a href="http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.1" rel="nofollow noreferrer">§4.1</a>). Because of this difference of having 2 numeric-convertible types, this rule from <a href="http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.12.2.7" rel="nofollow noreferrer">§15.12.2.7</a> applies:</p> <blockquote> <ul> <li><p>Otherwise, if the second and third operands have types that are convertible (<a href="http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.1.8" rel="nofollow noreferrer">§5.1.8</a>) to numeric types, then there are several cases: ...</p> <ul> <li>Otherwise, binary numeric promotion (<a href="http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.6.2" rel="nofollow noreferrer">§5.6.2</a>) is applied to the operand types, and the type of the conditional expression is the promoted type of the second and third operands. Note that binary numeric promotion performs <strong>unboxing conversion</strong> (<a href="http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.1.8" rel="nofollow noreferrer">§5.1.8</a>) and value set conversion (<a href="http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.1.13" rel="nofollow noreferrer">§5.1.13</a>).</li> </ul></li> </ul> </blockquote> <p>Because of the unboxing conversion performed on the result of the f2 inner conditional (null), a NullPointerException is raised.</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. 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