About Question enthuware.ocajp.i.v7.2.1036 :
Moderator: admin
-
- Posts: 32
- Joined: Wed Mar 14, 2012 5:45 pm
- Contact:
About Question enthuware.ocajp.i.v7.2.1036 :
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.
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.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1036 :
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.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 37
- Joined: Thu Feb 13, 2014 12:58 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1036 :
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.
-
- Posts: 8
- Joined: Wed Sep 10, 2014 2:42 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1036 :
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:
I am not expert in Java, just beginning to learn, so please teach me if I am wrong.
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
-
- Posts: 2
- Joined: Thu Dec 04, 2014 12:37 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1036 :
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
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
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1036 :
[] 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 ).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
If you like our products and services, please help us by posting your review here.
-
- Posts: 32
- Joined: Sat May 02, 2015 11:57 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1036 :
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] ] );
}
}
a [ (a = b)[0] ] 3
a [ (a = b)[1] ] 0
a [ (a = b)[2] ] 3
a [ (a = b)[3] ] 2
-
- Posts: 7
- Joined: Mon Oct 12, 2015 9:58 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1036 :
subhamsdalmia wrote:Output: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] ] ); } }
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 :
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 :
I got lost here: a [ (a = b)[1] ] 0subhamsdalmia wrote:Output: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] ] ); } }
a [ (a = b)[0] ] 3
a [ (a = b)[1] ] 0
a [ (a = b)[2] ] 3
a [ (a = b)[3] ] 2
I'm getting 4 instead of 0. Can someone please explain in detail how it resulted to 0?
-
- Posts: 9
- Joined: Wed Dec 28, 2016 12:35 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1036 :
Because of the previous line. The variable a (array) now points to the object that b points to.Deleted User 3513 wrote:I got lost here: a [ (a = b)[1] ] 0subhamsdalmia wrote:Output: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] ] ); } }
a [ (a = b)[0] ] 3
a [ (a = b)[1] ] 0
a [ (a = b)[2] ] 3
a [ (a = b)[3] ] 2
I'm getting 4 instead of 0. Can someone please explain in detail how it resulted to 0?
Did you execute all lines of code, or just one?
Re: About Question enthuware.ocajp.i.v7.2.1036 :
Hi Tanooki,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?
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.
-
- Posts: 4
- Joined: Wed Dec 14, 2016 11:07 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1036 :
Deleted User 3513 wrote:I got lost here: a [ (a = b)[1] ] 0subhamsdalmia wrote:Output: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] ] ); } }
a [ (a = b)[0] ] 3
a [ (a = b)[1] ] 0
a [ (a = b)[2] ] 3
a [ (a = b)[3] ] 2
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 :
Thank you very much! I get it now.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.
Who is online
Users browsing this forum: No registered users and 68 guests