• 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

Problem with Stringbuilder

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why does the following program print s=aaa sb=bbbaaabbb. It will print s=aaa because Strings are immutable and whatever changes we do to str in TestRefs method will not be reflected back in the main.
But i want to know why will it print sb=bbbaaabbb.Whatever changes we do to stringbuilder in testRefs is reflected back in the main.So after line sb.append(str) we are setting sb=null , so how come it prints sb=bbbaaabbb when it's been set to null.

 
Ranch Hand
Posts: 216
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

why will it print sb=bbbaaabbb.Whatever changes we do to stringbuilder in testRefs is reflected back in the main



This line should be like..

Whatever changes we do to stringbuilder object in testRefs is reflected back in the main



So, now you have correct statement. What is happening here, Can you figure it out now ?
 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nikhil Sagar wrote:

why will it print sb=bbbaaabbb.Whatever changes we do to stringbuilder in testRefs is reflected back in the main



This line should be like..

Whatever changes we do to stringbuilder object in testRefs is reflected back in the main



So, now you have correct statement. What is happening here, Can you figure it out now ?


By setting sb=null , we are saying that sb will not point to anything.So when it does not point to anything how does it print sb=bbbaaabbb ?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rohan Deshmkh wrote:why does the following program print s=aaa sb=bbbaaabbb. It will print s=aaa because Strings are immutable and whatever changes we do to str in TestRefs method will not be reflected back in the main.



It's true that String is immutable, but that's not why main() doesn't see the changes. The reason is that Java passes only by value. Primitives are passed by value and references are passed by value. This means that when you change the value of a parameter inside a method, the caller doesn't see the change, because the method parameter's copy of that variable is completely separate from the caller's copy.

Note that both the caller's variable and the method parameter point to the same object though. The object doesn't get copied, just the reference.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nikhil Sagar wrote:

why will it print sb=bbbaaabbb.Whatever changes we do to stringbuilder in testRefs is reflected back in the main



This line should be like..

Whatever changes we do to stringbuilder object in testRefs is reflected back in the main



That's kind of confusing wording.

If we change the value of the sb variable--that is, assign a different reference value to sb, so that it points to a different object--then the caller will not see that, since our variable is completely independent from the caller's.

If we change the state (the "contents") of the object that sb points to, such as with sb.append, then the caller WILL see that, since both variables point to the same object.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[quote=Rohan Deshmkh
By setting sb=null , we are saying that sb will not point to anything.So when it does not point to anything

Only our local copy of sb. It does not affect ther caller's variable. They're two completely separate, unrelated, independent variables that pointed to the same object when the method was called, but by setting sb to null inside the method, we're just saying that this particular variable now does not point to any object--we're not affecting the caller's variable, and we're not affecting the object.
 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Jeff Ok thanks, i got it.
 
Get out of my mind! Look! A tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic