• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

What's the answer?

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

The answer after the self test is 15 15
but I'm not so sure about this. Can somebody tell me why? Thank you!

([C0DE][/C0DE] tags added)
[ July 19, 2004: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming that a[1] should be a1[1] the program outputs 15 15 when compiled and run. What did you expect to get?


By the way, please take a look at JavaRanch's Naming Policy and change your displayed name accordingly.
Thanks
-Barry
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The array object is created in the heap. The value of the address of the array object is passed to the fix() method. The value of an element is chaged in the method and the same address of the array object is returned back to the main method where it is assigned back to a2. So essentially a1 and a2 are pointing to the same array-object on the heap.


You have passed the address to the array object to the fix method


The first element of the array object now holds 7, so the modified array
holds {3,7,5} and you return back the address of the array. Both a1 and a2 point to the same array object on the heap.

So now when you add the elements of the array, it holds 3+7+5=15
 
Hey cool! They got a blimp! But I have a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic