Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>A d = new A().aob = new A(); </code></pre> <p>In Java, the assignment operator is right-associative, i.e. they are evaluated from right-to-left. But also, they are in the least precedence operators group.</p> <p>So the second new operator (right of the second equality) is evaluated first and we get a new A object; let's say '<strong>a</strong>'. Now we have:</p> <pre><code>new A().aob = a; </code></pre> <p>The trick here is to recognize the operator precedence. Take a look at here: <a href="http://pages.cs.wisc.edu/~willb/cs302/spring-07/java-operator-precedence.pdf" rel="nofollow">http://pages.cs.wisc.edu/~willb/cs302/spring-07/java-operator-precedence.pdf</a></p> <p>The 'new' operator and the '.' method call operator have the same precedence but their associative quality are reversed: 'new' is right-associative and '.' is left-associative.</p> <p>So the compiler first applies the new operator on the 'right operand' which is here 'A()' (before the next operand comes into place). Let's call the new object <strong>b</strong>; and we have:</p> <pre><code>A d = b.aob = a; </code></pre> <p>The compiler now needs to apply the '.' operator first (as '.' has higher precedence than '=' operator). Let's call the object referred by 'b.aob' as <strong>c</strong>:</p> <pre><code>A d = c = a; </code></pre> <p>Finally all that is left are the assignment operators and they are right-associative (evaluated from right-to-left). So, <strong>a</strong> is assigned to <strong>c</strong> (b.aob) first, and then <strong>c</strong> is assigned to <strong>d</strong>.</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. 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