Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String and String Buffer

 
Angela lewis
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Ans is :s=aaa sb=bbbaaabbb

The value of String object is understandable because strings are immutable so any changes made in the mehod will not effect the original String.
But why does setting the StringBuffer to null does not effect its value.
After all StringBuffr is not immutable and any changes made to it in the method should have an effect in the original StringBuffer object.
i know i am not getting it right.
please help
 
Geoffrey Vlassaks
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,



After the append, the object where "sb" references to has a value of "bbbaaabbb". "sb = null;" removes the reference to that object, but the original "sb" (that one in the main method) still refers to that object. The whole clue is, that when you pass an object as an argument to a method, you pass a copy of the reference to that object. So by setting the copy reference to null, doesn't mean that the original will be set to null..

Greetz,
 
Swamy Nathan
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first of al have a look at this example on a different topic.

parameter passing

Its a pretty neat illustration.
 
Jerry Bustamente
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am overwhelmed by the postitive spirt of this forum and by the usefulness of JavaRanch.

I am new to Java and just wanted to thank Swamy for his excellent Flash application that demonstrates how variables are passed in Java.

http://www.geocities.com/mcglonec1978/javacert/paramPassing.html

I hope I can contribute to others benefit in the future once I am more skillful.

Jerry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic