• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Array

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

class Q55
{
public static void main(String[] args)
{
int[] a={11,12,13,14};
int[] b={0,1,2,3};
System.out.println(a[(a=b)[3]]);//1
}
}
The answer is 14. The assignments a=b, is local to the print statement??
 
Ranch Hand
Posts: 179
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No the assignment is to the complete method.This can be verified using the hashcode, of a and b.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The interesting thing is that even though 'a' acts like it has been equated to 'b' during the inner evaluation, i.e. (a=b)[3], part of the expression (resulting in a value of 3), it retains it's old identity in the outer evaluation part of the expression (resulting in a value of 14), then going forward it reverts to being equal to 'b' again, as shown in the extra line added below.

----------------------------------------------
class Q55 {
public static void main(String[] args) {
int[] a={11,12,13,14};
int[] b={0,1,2,3};
System.out.println(a[(a=b)[3]]); // prints 14 (from old a array)
System.out.println(a[0]); // prints 0 (from new a array)
}
}
----------------------------------------------
 
Marshal
Posts: 75842
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That presumably means the [] operator takes precedence over the = inside.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still don't understand how a[(a=b)[3]] evaluates to 14. Can someone please explain? I understand that a[3] is 14. What does the assignment of a=b evaluate to?
Ravikanth could you please explain what you mean by "No the assignment is to the complete method.This can be verified using the hashcode, of a and b"?
And Patrick how can it print 0 for--> System.out.println(a[0]); // prints 0 (from new a array)
since the assignment is only in the print statement?
Thanks,
Meera
 
meera kanekal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I think I understand what is going on here.
(a=b)[3] evaluates to 3. So a[(a=b)[3]] is 14.
But I thought the assignment a=b would be local. apparently not. Could someone explain why it is not?
Thanks,
Meera
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Local things happens between { } not between ( ).

I knew that you could use ( ) everywhere in Java, but that is ugly O_O
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic