Can you please break down the explanation to the answer to this question even further, so that my slow-to-understand brain will understand?
Given the code in this question, what does the expression "(a=b)[3] " evaluate to? and, whatever that value is, is that value supposed to be the subscript used when referring to "a[(a=b)[3]]"?
Thank you
Gary
About Question com.enthuware.ets.scjp.v6.2.179 :
Moderator: admin
-
- Site Admin
- Posts: 10398
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.179 :
Consider that you have two objects in memory somewhere. At Memory location 10000, you have OBJECT 1 (which is an array of ints containing 1, 2, 3, 4) and at Memory location 20000, you have OBJECT 2(which is an array of ints containing 2, 3, 1, 0) .
To begin with, the variable 'a' is pointing to OBJECT 1 and 'b' is pointing to OBJECT 2. This means, the value contained by variable a is 10000 and by variable b is 20000. (If you do not understand this statement that means, you first need to learn what is a reference Try http://javadude.com/articles/passbyvalue.htm or http://www.dickbaldwin.com/java/Java020.htm).
Now,
a[ (a = b) [3] ] is an expression. To find the value of this expression, JVM starts evaluating it from left.
1. It sees variable a pointing at OBJECT 1. The JVM makes a note of this fact. Think of this as JVM storing the value of ‘a’ i.e. the address of memory location 10000 in a temporary variable (T1). So now you have T1 pointing to the same object at memory location 10000.
2. Now, it sees (a=b)[3]. The variable ‘b’ contains the address 20000, which it assigns to a. So the value of a is now changed to 2000, which means, a is now pointing to the object at location 20000. Observe that T1 is still pointing to OBJECT 1.
3. Now, it sees [3]. So it applies [3] to whatever is pointed to by ‘a’, which Object 2 at memory location 2000. Thus, it figures out that the value of Object 2 [3] is actually 0. Thus, the value of expression (a=b)[3] is 0
4. So it applies 0 to the result of evaluation of Step 1, which is T1. T1[0] is 1.
HTH,
Paul.
To begin with, the variable 'a' is pointing to OBJECT 1 and 'b' is pointing to OBJECT 2. This means, the value contained by variable a is 10000 and by variable b is 20000. (If you do not understand this statement that means, you first need to learn what is a reference Try http://javadude.com/articles/passbyvalue.htm or http://www.dickbaldwin.com/java/Java020.htm).
Now,
a[ (a = b) [3] ] is an expression. To find the value of this expression, JVM starts evaluating it from left.
1. It sees variable a pointing at OBJECT 1. The JVM makes a note of this fact. Think of this as JVM storing the value of ‘a’ i.e. the address of memory location 10000 in a temporary variable (T1). So now you have T1 pointing to the same object at memory location 10000.
2. Now, it sees (a=b)[3]. The variable ‘b’ contains the address 20000, which it assigns to a. So the value of a is now changed to 2000, which means, a is now pointing to the object at location 20000. Observe that T1 is still pointing to OBJECT 1.
3. Now, it sees [3]. So it applies [3] to whatever is pointed to by ‘a’, which Object 2 at memory location 2000. Thus, it figures out that the value of Object 2 [3] is actually 0. Thus, the value of expression (a=b)[3] is 0
4. So it applies 0 to the result of evaluation of Step 1, which is T1. T1[0] is 1.
HTH,
Paul.
-
- Posts: 50
- Joined: Sun Nov 10, 2013 4:39 am
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.179 :
Did some more investigation into this area and run into something I can't explain:
The output is:
[4, 5, 6]
[4, 5, 6]
where I was expecting:
[4, 5, 6]
[4, 4, 6]
Creating this message I realized that the probably answer is:
JVM first gets and memorize reference to w[1] which is the adress of value 2.
Then w = t replaces the reference w with a reference to t.
w[0] is then pointing to value 4, but this value is not put in the new w[1] but in the memorized w[1].
So actually the original w-int-array is changed and becomes {1, 4, 3}.
Am I right?
Code: Select all
import java.util.Arrays;
public class Initialization2 {
public static void main(String[] args) {
int[] t = {4, 5, 6};
int[] w = {1, 2, 3};
w[1] = (w = t)[0];
System.out.println(Arrays.toString(t));
System.out.println(Arrays.toString(w));
}
}
[4, 5, 6]
[4, 5, 6]
where I was expecting:
[4, 5, 6]
[4, 4, 6]
Creating this message I realized that the probably answer is:
JVM first gets and memorize reference to w[1] which is the adress of value 2.
Then w = t replaces the reference w with a reference to t.
w[0] is then pointing to value 4, but this value is not put in the new w[1] but in the memorized w[1].
So actually the original w-int-array is changed and becomes {1, 4, 3}.
Am I right?
-
- Site Admin
- Posts: 10398
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.179 :
Yes, you are right. This is also explained in Section 15.26.1 of JLS here: http://docs.oracle.com/javase/specs/jls ... #jls-15.26
Paul.
HTH,If the left-hand operand is an array access expression (§15.13), possibly enclosed in one or more pairs of parentheses, then:
First, the array reference subexpression of the left-hand operand array access expression is evaluated. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason; the index subexpression (of the left-hand operand array access expression) and the right-hand operand are not evaluated and no assignment occurs.
Paul.
Who is online
Users browsing this forum: No registered users and 7 guests