Page 1 of 1
About Question enthuware.ocajp.i.v7.2.1036 :
Posted: Fri Mar 08, 2013 3:36 pm
by baptize
why 'a' is remembered when this is done at runtime?
after the expression (a = b)[3] is evaluated JVM knows that 'a' now refers to 'b' so it should be b[3], no? or is it Sun pulling a rabbit from a hat??
this reminds me of finally tries to alter a value that catch() would return.
Re: About Question enthuware.ocajp.i.v7.2.1036 :
Posted: Fri Mar 08, 2013 4:57 pm
by admin
It works that way because that is how it was designed
There is no particularly great reason for it. You may want to check out the posts in this thread as well:
viewtopic.php?f=2&t=1056
-Paul.
Re: About Question enthuware.ocajp.i.v7.2.1036 :
Posted: Sun Mar 09, 2014 11:30 pm
by fasty23
as the right answer is "It will print 1" I think the question needs to have a choice "It will print 2" too. because it could be another chose to challenge test takers.
Re: About Question enthuware.ocajp.i.v7.2.1036 :
Posted: Tue Sep 16, 2014 10:24 pm
by thchuong
I think the reason is because JVM stores copies of ALL intermediate values during the process of evaluating expression. And it processes from left to right.
So I have the easy way to determine the final result: Make the tree of expression.
Example:
Code: Select all
int z = 1; z = z + (z=2)*3 + z;
Tree:
+
/ \
/ \
+ z
/ \
/ \
z *
/ \
/ \
z = 2 3
Here the JVM accesses z and stores 1 in the list of intermediate values.
+
/ \
/ \
+ z
/ \
/ \
1 *
/ \
/ \
z = 2 3
Then it assigns 2 to the ACTUAL z. Then stores 2 in the list of intermediate values.
+
/ \
/ \
+ z
/ \
/ \
1 *
/ \
/ \
2 3
Enough info for the multiply action, so it calculates 2*3=6:
+
/ \
/ \
+ z
/ \
/ \
1 6
Then 1 + 6 = 7;
+
/ \
/ \
7 z
Then it ACCESSes z again, this time z has the value of 2 ,and it stores 2 in the list of intermediate values.
+
/ \
/ \
7 2
Finally 7 + 2 = 9!
If we apply the principle above to this question, it is the same (note that a is a reference variable)
[]
/ \
/ \
a []
/ \
/ \
a = b 3
I am not expert in Java, just beginning to learn, so please teach me if I am wrong.
Re: About Question enthuware.ocajp.i.v7.2.1036 :
Posted: Sun Dec 07, 2014 6:15 am
by hadesgrid@gmail.com
Hello,
in the (a=b)[3]
what does the [3] do
i konw that a=b asings b to a
but [3] ?
thank you in advance
Re: About Question enthuware.ocajp.i.v7.2.1036 :
Posted: Sun Dec 07, 2014 9:06 am
by admin
hadesgrid@gmail.com wrote:Hello,
in the (a=b)[3]
what does the [3] do
i konw that a=b asings b to a
but [3] ?
thank you in advance
[] syntax is to refers to a particular element of an array. Since a refers to an array, a[3] refers to the 4th element in that array ( 0, 1, 2, 3 ).
Re: About Question enthuware.ocajp.i.v7.2.1036 :
Posted: Thu May 21, 2015 7:59 am
by subhamsdalmia
Code: Select all
public class P11
{
public static void main(String[ ] args)
{
int[] a = { 1, 2, 3, 4 };
int[] b = { 2, 3, 1, 0 };
System.out.println( " a [ (a = b)[0] ] "+a [ (a = b)[0] ] );
System.out.println( " a [ (a = b)[1] ] "+a [ (a = b)[1] ] );
System.out.println( " a [ (a = b)[2] ] "+a [ (a = b)[2] ] );
System.out.println( " a [ (a = b)[3] ] "+a [ (a = b)[3] ] );
}
}
Output:
a [ (a = b)[0] ] 3
a [ (a = b)[1] ] 0
a [ (a = b)[2] ] 3
a [ (a = b)[3] ] 2
Re: About Question enthuware.ocajp.i.v7.2.1036 :
Posted: Tue Oct 27, 2015 12:52 pm
by klhlubek
subhamsdalmia wrote:Code: Select all
public class P11
{
public static void main(String[ ] args)
{
int[] a = { 1, 2, 3, 4 };
int[] b = { 2, 3, 1, 0 };
System.out.println( " a [ (a = b)[0] ] "+a [ (a = b)[0] ] );
System.out.println( " a [ (a = b)[1] ] "+a [ (a = b)[1] ] );
System.out.println( " a [ (a = b)[2] ] "+a [ (a = b)[2] ] );
System.out.println( " a [ (a = b)[3] ] "+a [ (a = b)[3] ] );
}
}
Output:
a [ (a = b)[0] ] 3
a [ (a = b)[1] ] 0
a [ (a = b)[2] ] 3
a [ (a = b)[3] ] 2
You can see here very good, that a = b has an effect on a after the statements. Thank you for your great example
Re: About Question enthuware.ocajp.i.v7.2.1036 :
Posted: Tue Jan 19, 2016 8:29 pm
by Deleted User 2655
values of array b is assigned to a.
Code: Select all
public class P11
{
public static void main(String[ ] args)
{
int[] a = { 1, 2, 3, 4 };
int[] b = { 2, 3, 1, 0 };
System.out.println( " a [ (a = b)[0] ] "+a [ (a = b)[0] ] );
System.out.println( " a first "+a[0]);
System.out.println( " a first "+a[1]);
System.out.println( " a first "+a[2]);
System.out.println( " a first "+a[3]);
System.out.println( " ************************");
System.out.println( " b first "+b[0]);
System.out.println( " a first "+a[1]);
System.out.println( " a first "+a[2]);
System.out.println( " a first "+a[3]);
System.out.println( " a [ (a = b)[1] ] "+a [ (a = b)[1] ] );
System.out.println( " a second "+a[0]);
System.out.println( " a second "+a[1]);
System.out.println( " a second "+a[2]);
System.out.println( " a second "+a[3]);
System.out.println( " ************************");
System.out.println( " b second "+b[0]);
System.out.println( " a second "+a[1]);
System.out.println( " a second "+a[2]);
System.out.println( " a second "+a[3]);
System.out.println( " a [ (a = b)[2] ] "+a [ (a = b)[2] ] );
System.out.println( " a third "+a[0]);
System.out.println( " a third "+a[1]);
System.out.println( " a third "+a[2]);
System.out.println( " a third "+a[3]);
System.out.println( " ************************");
System.out.println( " b third "+b[0]);
System.out.println( " a third "+a[1]);
System.out.println( " a third "+a[2]);
System.out.println( " a third "+a[3]);
System.out.println( " a [ (a = b)[3] ] "+a [ (a = b)[3] ] );
System.out.println( " a fourth "+a[0]);
System.out.println( " a fourth "+a[1]);
System.out.println( " a fourth "+a[2]);
System.out.println( " a fourth "+a[3]);
System.out.println( " ************************");
System.out.println( " b fourth "+b[0]);
System.out.println( " a fourth "+a[1]);
System.out.println( " a fourth "+a[2]);
System.out.println( " a fourth "+a[3]);
}
}
Re: About Question enthuware.ocajp.i.v7.2.1036 :
Posted: Wed Dec 28, 2016 10:45 am
by Deleted User 3513
subhamsdalmia wrote:Code: Select all
public class P11
{
public static void main(String[ ] args)
{
int[] a = { 1, 2, 3, 4 };
int[] b = { 2, 3, 1, 0 };
System.out.println( " a [ (a = b)[0] ] "+a [ (a = b)[0] ] );
System.out.println( " a [ (a = b)[1] ] "+a [ (a = b)[1] ] );
System.out.println( " a [ (a = b)[2] ] "+a [ (a = b)[2] ] );
System.out.println( " a [ (a = b)[3] ] "+a [ (a = b)[3] ] );
}
}
Output:
a [ (a = b)[0] ] 3
a [ (a = b)[1] ] 0
a [ (a = b)[2] ] 3
a [ (a = b)[3] ] 2
I got lost here: a [ (a = b)[1] ] 0
I'm getting 4 instead of 0. Can someone please explain in detail how it resulted to 0?
Re: About Question enthuware.ocajp.i.v7.2.1036 :
Posted: Wed Dec 28, 2016 12:44 pm
by Tanooki
Deleted User 3513 wrote:subhamsdalmia wrote:Code: Select all
public class P11
{
public static void main(String[ ] args)
{
int[] a = { 1, 2, 3, 4 };
int[] b = { 2, 3, 1, 0 };
System.out.println( " a [ (a = b)[0] ] "+a [ (a = b)[0] ] );
System.out.println( " a [ (a = b)[1] ] "+a [ (a = b)[1] ] );
System.out.println( " a [ (a = b)[2] ] "+a [ (a = b)[2] ] );
System.out.println( " a [ (a = b)[3] ] "+a [ (a = b)[3] ] );
}
}
Output:
a [ (a = b)[0] ] 3
a [ (a = b)[1] ] 0
a [ (a = b)[2] ] 3
a [ (a = b)[3] ] 2
I got lost here: a [ (a = b)[1] ] 0
I'm getting 4 instead of 0. Can someone please explain in detail how it resulted to 0?
Because of the previous line.
The variable a (array) now points to the object that b points to.
Did you execute all lines of code, or just one?
Re: About Question enthuware.ocajp.i.v7.2.1036 :
Posted: Wed Dec 28, 2016 6:01 pm
by Deleted User 3513
Tanooki wrote:
Because of the previous line.
The variable a (array) now points to the object that b points to.
Did you execute all lines of code, or just one?
Hi Tanooki,
I did this manually with a pen and paper. First, I got 3 correctly but after executing the 2nd output, I don't quite understand why it results to 0. Can you please help me understand what happens in the 2nd line(a [ (a = b)[1] ] 0) exactly?
Thanks in advance.
Re: About Question enthuware.ocajp.i.v7.2.1036 :
Posted: Thu Dec 29, 2016 2:47 pm
by JavaJunky
Deleted User 3513 wrote:subhamsdalmia wrote:Code: Select all
public class P11
{
public static void main(String[ ] args)
{
int[] a = { 1, 2, 3, 4 };
int[] b = { 2, 3, 1, 0 };
System.out.println( " a [ (a = b)[0] ] "+a [ (a = b)[0] ] );
System.out.println( " a [ (a = b)[1] ] "+a [ (a = b)[1] ] );
System.out.println( " a [ (a = b)[2] ] "+a [ (a = b)[2] ] );
System.out.println( " a [ (a = b)[3] ] "+a [ (a = b)[3] ] );
}
}
Output:
a [ (a = b)[0] ] 3
a [ (a = b)[1] ] 0
a [ (a = b)[2] ] 3
a [ (a = b)[3] ] 2
I got lost here: a [ (a = b)[1] ] 0
I'm getting 4 instead of 0. Can someone please explain in detail how it resulted to 0?
@Deleted User 3513
after you print the first print statement. Your array a is replaced by array b. So now when you refer array a the next, this is actually an array b. Now your question why a[(a=b)[1]] =0 ? remember your array a has been changed here already to {2,3,1,0} and array b is also {2,3,1,0}. when you do (a=b)[1]--> this gives you 3. now a[(a=b)[1]] -->a[3] which is the fourth element of an array a[3]=0. Hope that help you. I spent some time on this too.
Re: About Question enthuware.ocajp.i.v7.2.1036 :
Posted: Thu Dec 29, 2016 3:56 pm
by Deleted User 3513
JavaJunky wrote:
@Deleted User 3513
after you print the first print statement. Your array a is replaced by array b. So now when you refer array a the next, this is actually an array b. Now your question why a[(a=b)[1]] =0 ? remember your array a has been changed here already to {2,3,1,0} and array b is also {2,3,1,0}. when you do (a=b)[1]--> this gives you 3. now a[(a=b)[1]] -->a[3] which is the fourth element of an array a[3]=0. Hope that help you. I spent some time on this too.
Thank you very much! I get it now.