• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array doubt

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is from Enthuware mock exam.


class Array1{
public static void main(String [] args){
int [] a = {1,2,3,4};
int [] b = {2,3,1,0};
System.out.println(a[(a=b)[3]]);
}
}

The answer is it will print 1. I read the explanation but still not clear about it. Can you anyone please explain? Will this be executed as a[0][3]?

Thanks

 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(a=b) is evaluated first. For example : int[] c = (a=b); Here, a,b and c all refer to the b array.
Now, (a=b)[3] returns the fourth element of the array returned by (a=b), which is {2,3,1,0}. The fourth element is 0.
Finally, a[(a=b)[3]] is the same as a[0], thus the answer is 1.
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
very well explained Christophe sir
 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Christophe Verré wrote:(a=b) is evaluated first. For example : int[] c = (a=b); Here, a,b and c all refer to the b array.
Now, (a=b)[3] returns the fourth element of the array returned by (a=b), which is {2,3,1,0}. The fourth element is 0.
Finally, a[(a=b)[3]] is the same as a[0], thus the answer is 1.



But since a is assigned a reference b as a consequence of the statement (a=b), then a is pointing to array b and {1,2,3,4} should be abondoned.

Then a[0] should be 2 and not 1.

But i checked , the output is 1. But i am not clear with explanation yet !!!
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you try out the following



The answer is in accordance to mine ie 2 in this case, but why not in given question ???

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sahil wrote:But since a is assigned a reference b as a consequence of the statement (a=b), then a is pointing to array b and {1,2,3,4} should be abondoned.


You noticed a very important point that I failed to explain. The assignment of "b" to "a" happens last. It's a bit like the PostIncrementOperatorAndAssignment. The value of "a" will not be changed until the statement ends. Try this :

The result will be 1, 2.
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yaa got it, but a lil Strange of JAVA !!!
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


evaluation and operator precedence are different thing . evaluation always happens left to right (except assignment and preincrement operators).

so, in first statement hence a is already evaluated the output is 1.

hth
 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


why do we get the output
2
from the
System.out.println(a[(a=b)[3]]); statement ?

please explain

 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


 
Ranch Hand
Posts: 384
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:




a[(a=b)[3]] is just like a(b[3]) since a=b so it prints '1' a b[3] evaluates to '0' so

a[(a=b)[3]] = a[b[3]] = a[0] = '1'

now the assignment of a = b takes place and then on applying a[0] it prints 2 since now a points to b
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lalit Mehra wrote:

Abimaran Kugathasan wrote:




a[(a=b)[3]] is just like a(b[3]) since a=b so it prints '1' a b[3] evaluates to '0' so

a[(a=b)[3]] = a[b[3]] = a[0] = '1'

now the assignment of a = b takes place and then on applying a[0] it prints 2 since now a points to b



Yup, That's why I mentioned it above!
 
Lalit Mehra
Ranch Hand
Posts: 384
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:

Yup, That's why I mentioned it above!



i thought i should give an explanation ...
 
reply
    Bookmark Topic Watch Topic
  • New Topic