This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String and StringBuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "String and StringBuffer" Watch "String and StringBuffer" New topic
Author

String and StringBuffer

Madhu Sudhana
Ranch Hand

Joined: Apr 16, 2006
Posts: 127
Hi
I heard that String is immutable and StringBuffer is immutable?
what is it actually meant?
and how they are internally referenced?

thanks in advance


"And the trouble is, if you don't risk anything, you risk even more." -- Erica Jong.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14672
    
  11

Please refer to the following article :
http://www.javaranch.com/journal/200409/Journal200409.jsp#a1


[My Blog]
All roads lead to JavaRanch
Abdullah Mamun
Ranch Hand

Joined: Mar 19, 2007
Posts: 99

I hope you actually meant "StringBuffer is mutable"

Immutable means after creation you cannot change the object which you can do with mutable object.

For example in case of string,

this will return a new String after concatenation.

But in case of StringBuffer,

this will return the actual object after appending the " world!!!" at the end.


MooN
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3327

Mutable -> ability to change the object (in this case, the content of the object) once its created. ex. java.lang.StringBuffer

Immutable -> inability to change the object. example. java.lang.String

Lets say, the Person object and Name Object. The person object can have any name but once a name is registered for a Person object, that name cannot be changed. In this case, the name is an IMMUTABLE object. But the Person object is MUTABLE object.

Once a situation arises for a person to change his name based on numerology, the original name he was having CANNOT be changed. It has to be lost and a totally new name has to be assigned to the person even if there is a small letter change in his original name. To the gazette and other systems, it will be a new name only NOT THE MODIFIED ORIGINAL name. In this way, the name object is IMMUTABLE.

But the person object can be altered by changing the name, colour, clothings etc. Means, the same object CAN be changed by changing its properties. In this way, the person object is MUTABLE.

In case of Strings and StringBuffers,



The above code produces the following output:



Though it seems that both String and StringBuffer objects are altered, but actually only the StringBuffer object is altered and NOT the String Object.

There are TWO String Objects present (one is the actual "Sample" and another one is appended String "Sample String") showing that the original String object having the content "Sample" is IMMUTABLE.

HtH.
[ July 06, 2007: Message edited by: Raghavan Muthu ]

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Zoram Paul
Ranch Hand

Joined: Jul 10, 2007
Posts: 59
Great answer Raghavan..
That was a great help.
Zoram Paul
Ranch Hand

Joined: Jul 10, 2007
Posts: 59
Great answer Raghavan..
That was a great help.
AtulKumar Gaur
Ranch Hand

Joined: Jun 24, 2007
Posts: 40
HI
Raghwan I do agree with you that it creates two object at pool but could you tell ne that how can we access the actual object "Sample".

Thanks in Advance
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3327


Raghwan I do agree with you that it creates two object at pool but could you tell ne that how can we access the actual object "Sample".


If at all, the Garbage Collector had not run, we can get access to the String "Sample" by assigning the same String Object to the another String reference.

But if you give it via new operator, it may be a new String until and unless you invoke intern() method on the String reference.

HtH.
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
ok!!!Raghavan you have mentioned that two objects are getting created as if String is immutable but how the value is getting stored in same object...?

like in the below code

String s="sample"
s+="test"

in this case also 2 objects get created i agree with this,but value is getting saved in single object s right.......than it means that value of object s is changing... although 2 object of same name s are created but all in all value of single object s gets changed.........
please explain this.......
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3327

actually at runtime, this is what happens.

  • a new StringBuffer is created with the content of the original string
  • the string to be appended is added with the content of StringBuffer
  • StringBuffer's toString method is invoked which obviously returns a new String.


  • Codewise, it goes like this...



    Hope this helps!
    dhwani mathur
    Ranch Hand

    Joined: May 08, 2007
    Posts: 621
    But raghavan my doubt is relevant to String which is immutable,not StringBuffer which is Mutable,isnt it?
    [ July 11, 2007: Message edited by: dhwani mathur ]
    Manfred Klug
    Ranch Hand

    Joined: Jun 04, 2007
    Posts: 377
    Originally posted by dhwani mathur:
    but value is getting saved in single object s right
    s is not an object!!! s is a reference to an object.
    victor kamat
    Ranch Hand

    Joined: Jan 10, 2007
    Posts: 247
    Use StringBuilder instead of StringBuffer.

    StringBuffer is synchronized but StringBuilder is not.
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3327


    Originally posted by dhwani mathur:
    But raghavan my doubt is relevant to String which is immutable,not StringBuffer which is Mutable,isnt it?


    My explanation was for your previous question


    String s="sample"
    s+="test"

    in this case also 2 objects get created i agree with this,but value is getting saved in single object s right.......than it means that value of object s is changing... although 2 object of same name s are created but all in all value of single object s gets changed.........

    please explain this.......


    Internally, thats what is getting happened. That way, the original String object being pointed by the reference variable "s" is unchanged AND "s" is now given to the new Object to refer at the end of concatenation operation.

    HtH.
    dhwani mathur
    Ranch Hand

    Joined: May 08, 2007
    Posts: 621
    well!!Raghavan what i understood is that now reference s is pointing to new value that too after concatenation........if i am right or not? please let me know.......
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3327


    Originally posted by dhwani mathur:
    what i understood is that now reference s is pointing to new value that too after concatenation........


    Yes, thats correct. But its happening behind the scenes. So you may think that the content of the String object is modified.

    But, the String object once created is NEVER EVER MODIFIED. Only the reference to the Object (in our case, "s") is being made point to a NEW String object created behind the scenes as a result of concatenation.

    HtH.
    prathibha ananthapadmanabha
    Greenhorn

    Joined: Jul 12, 2007
    Posts: 16
    Hi Raghavan,

    Thankyou for a very clear clarification on the difference between string and string buffer .
    It was of a great help .

    Regards,
    Prathibha.
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3327

    My Pleasure Prathibha
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3327

    Forgot to Welcome you to JavaRanch Prathibha
    dhwani mathur
    Ranch Hand

    Joined: May 08, 2007
    Posts: 621
    Thanks Raghavan!!!I am clear with my doubts ..........
    Zoram Paul
    Ranch Hand

    Joined: Jul 10, 2007
    Posts: 59
    Hi Raghavan Muthu,

    Why didn't you welcomed me?
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3327


    Originally posted by Zarom Paul:
    Hi Raghavan Muthu,

    Why didn't you welcomed me?


    By then, you were not a new comer to JavaRanch that's why. No issues, Welcoming you to JavaRanch Zarom!!
     
    I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
     
    subject: String and StringBuffer
     
    Similar Threads
    Immutable objects....
    StringBuffer question
    What Are the Differences Between String and StringBuffer
    How to know a class is Immutable or Not
    StringBuffer and String returned from a method doubt