• Post Reply Bookmark Topic Watch Topic
  • New Topic

String and StringBuffer  RSS feed

 
Madhu Sudhana
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please refer to the following article :
http://www.javaranch.com/journal/200409/Journal200409.jsp#a1
 
Abdullah Mamun
Ranch Hand
Posts: 99
Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Zoram Paul
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great answer Raghavan..
That was a great help.
 
Zoram Paul
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great answer Raghavan..
That was a great help.
 
AtulKumar Gaur
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 621
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 377
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 247
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Use StringBuilder instead of StringBuffer.

    StringBuffer is synchronized but StringBuilder is not.
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    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
    Posts: 621
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    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
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    My Pleasure Prathibha
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Forgot to Welcome you to JavaRanch Prathibha
     
    dhwani mathur
    Ranch Hand
    Posts: 621
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Raghavan!!!I am clear with my doubts ..........
     
    Zoram Paul
    Ranch Hand
    Posts: 59
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Raghavan Muthu,

    Why didn't you welcomed me?
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    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!!
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!