• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

One question

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I came across a question in a mock test which is as follows.

When run it prints "Java more" for a and "Rmi" for b.
But inside the swap()when I put a print statement I see the value of b is "Java more".why the change to variable a is reflected in main()and not the change to b?
Can some body explain?
Thanks
Liza
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Lisa
Inside methods we only have copies of references or primitive types.So in the method u assigend method's copy of "a" to be not the original "a" reference 'a' in main is still pointiong to original object .
I hope this helps
regards
 
denish mehta
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by denish mehta:
Hey Lisa
Inside methods we only have copies of references or primitive types.So in the method u assigend method's copy of "a" to "b" not the original "a" reference .'a' in main is still pointiong to original object .
I hope this helps
regards


 
Liza Misra
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But then Denish when we append to the variable a inside the method ,how come we get the changed value in main?both a and b should retain their unchanged values once they are out of scope.That is what confuses me.
 
denish mehta
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
U r missing the whole point here.In passing references as arguments the referneces does get copied not the object and the copied refernece still points to the same object.So any change u make using this copied reference to the object effects the object .U actually get two references to the same "object".
And also u assgined b=a after changing the String buffer
Regards
Denish
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Liza,
Both 'a' and 'b' are passed as copies of references to StringBuffer objects.
In the method, a.append(" more") changes the actual object pointed to by 'a'; the same object 'a' in main() points to.
However, the code 'b = a', 'b' is a copy of the original 'b' reference in main(). The assignment causes the method variable 'b' to point to the original 'a' object BUT it does not change the original reference variable 'b' in main().
Hope that helps. There's an article, Understanding that parameters are passed by value and not by reference that has a good explanation of how the values are passed.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Liza Misra
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jane and Denish.
REgards
Liza
 
The moustache of a titan! The ad of a flea:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic