• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Not understanding the output

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


I am not able to understand the above code . Why does it print 1?what does a=b do in above code?
 
Ranch Hand
Posts: 300
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Rohan,

In this code first assignment is evaluated which is a=b means value of array b is assigned to a but it will not change the value a its just evaluating.

Second thing is ((a = b)[3])) which means it will look for 4th element in array of a(which contains the value of b after evaluation).
At last it will a[(a = b)[3]] print 1 becuase it will print the 0th element from a.

Just seprate the brackets and you can see but make sure assignment is not done else it will change the value or array a.
Regards
jatan
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rohan Deshmkh wrote:
I am not able to understand the above code . Why does it print 1?what does a=b do in above code?


(a = b) just makes both the reference a and b point to the same array (Initially pointed by b)
So,
a[(a = b)[3]] is translated to a[b[3]], which will print 1

 
R. Jain
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
--
 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok thanks i got it.
a[(a=b)[3]] is equivalent to a[b[3]].
 
R. Jain
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rohan Deshmkh wrote:ok thanks i got it.
a[(a=b)[3]] is equivalent to a[b[3]].


But what here matters is how?? You should try to understand it..
You should probably make a diagram on paper with 2 boxes for array and a & b reference pointing to them..
And then see the change as yo proceed in the code.
 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes i got it.Because of (a=b) ,a will point to the same array as that of b while evalulating (a=b)[3]
 
R. Jain
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rohan Deshmkh wrote:yes i got it.Because of (a=b) ,a will point to the same array as that of b while evalulating (a=b)[3]


Also after the evaluation... try to print a[0] after this print statement..
 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a[0] will print 2 because a now refers to b array and b[0] is 2.
 
Ranch Hand
Posts: 163
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

R. Jain wrote:
a[(a = b)[3]] is translated to a[b[3]], which will print 1



Why wouldn't it translate to b[b[3]]? Doesn't the assignment evaluate first?
 
Marshal
Posts: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bill Clar wrote: . . . Doesn't the assignment evaluate first?

Probably yes, because it is in ().
 
Whose rules are you playing by? This tiny ad doesn't respect those rules:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic