• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

StringBuffer problem

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
question from http://www.geocities.com/sunjava_scjp/Mock2.html

Why is the output "String Buffer added" when String Buffer is assigned "Hai"
in the last line?

Pls explain
[ November 08, 2004: Message edited by: Barry Gaunt ]
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

When you enter in the method "method", there is only 1 object in memory (the one with the value "StringBuffer") but 2 references to this object (sb in the main andthe local variable sb in the method parameter).

Ref sb (from main method) ---> "StringBuffer"
Ref sb (from method method) ---> the same object

So when you call sb.append(...) in the method "method", it's the original object that is modified =>StringBufferAppend. At this time we have in memory:

Ref sb (from main method) ---> "StringBufferAppend"
Ref sb (from method method) ---> the same object

Then when you create a new object and assign it to sb, it is the local reference that is modified, not the reference from the variable of the main method. The result in memory is:

Ref sb (from main method) ---> "StringBufferAppend"
Ref sb (from method method) ---> "Hai"

So when you leave the method "method", the variable sb reference already the same object, that has been modified.

Hope it can help.
 
rimzim sinha
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for clearing my concepts!
 
Ranch Hand
Posts: 1880
Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your explanation Olivier Lambert.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
very nice explaination...!!
thanx bro
 
Don't play dumb with me! But you can try this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic