• 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

confuse about tricky references!

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!
There is something I don't understand about passing references:

Why the output is [14][]?
Since one and two are references, three and four should be point to the same objects within method(). I know that the scope of one and two is main() but references are passed to method() so any changes to the references in method() will affect one and two in main() (Is that correct?)
Why three.add(14) did what I was expecting but four = three didn't?
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All parameter passing in Java is by value. When you send an object reference to a method, only a copy of that reference is sent to the method. You can modify the contents of the object through that copy of the reference, however if you assign the formal parameter to point to a different object within the method, that does not affect the object referred to by the actual parameter.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually here the reference copies are passed to the method...when u change the references...they are local to the method.....that does not effect the actual arguments...but if u change the state of the reference ..as such u have done three.add(14)..its contents are effected ..and therefore 14 is added as a list element to one(List)...and hence when u came out of the method ...one is printing 14 and hence two is not changed ..empty element is printed...
hope u got it...
ranchers please correct me if i am wrong...
 
Enrique Villamizar
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi and thanks Keith:

When you say :

however if you assign the formal parameter to point to a different object within the method, that does not affect the object referred to by the actual parameter.

Do you mean that everything I do in the method (NOT including changing the reference) with the copy reference will affect one and two? For ex. if at the end of method() I add this won't affect one but now three points to null?
Please, let me know if I got it and thanks for your quickly reply. I like rancher for being fast and accurate!


Enrique Villamizar
P.S. English isn't my first language!
 
vandu matcha
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes exactly ....even if u point three to null...it does not effect one ....
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, partly. The exceptions are immutable objects like String, Integer, etc. Since they are immutable, if you pass a reference to them to a method, then the only thing you can do is use the object pointed to by the reference. You can't change the object.
 
We cannot change unless we survive, but we will not survive unless we change. Evolving tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic