my dog learned polymorphism
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String and String Buffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String and String Buffer" Watch "String and String Buffer" New topic

String and String Buffer

Angela lewis
Ranch Hand

Joined: Mar 01, 2004
Posts: 100

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

Joined: May 12, 2004
Posts: 24

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..

Swamy Nathan
Ranch Hand

Joined: May 16, 2004
Posts: 187
first of al have a look at this example on a different topic.

parameter passing

Its a pretty neat illustration.
Jerry Bustamente
Ranch Hand

Joined: May 24, 2004
Posts: 90
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.

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

I agree. Here's the link:
subject: String and String Buffer
It's not a secret anymore!