• 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

Different behavior when accessing array through method call or directly

 
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

With a big surprise, I discovered that the behavior is different depending on whether you access an Array directly or through a method call..
Does someone know why ?
I suspect there is a stack thing somewhere but I can't explain why I have this result because an Array is an Object




 
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Each call to your myArray() method creates a new int[]. so the one that you increment index 0 on line 9 is not the same array that you then print index 0 on line 10.
 
Alion Bada
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice one man !
I didn't pay attention to the "new" ... I was so impressed by the way of accessing an array through method name...(myArray()[0] )
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alion Bada wrote:I was so impressed by the way of accessing an array through method name...(myArray()[0] )


I agree the syntax seems a little bit weird, but actually there's no real difference between this and method chaining as in the following code snippetThe append() method returns a StringBuilder, so you can invoke any StringBuilder method on a method invocation. And that's how you can chain different method invocations. And the same applies to your example: the myArray() method returns a reference variable to an array, so any method and/or operator you can invoke on such a reference variable can be used in combination with the method invocation as well. Writing myArray()[0]++; is functionally equivalent with
 
machines help you to do more, but experience less. Experience this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic