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.
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.
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
posted
0
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.......
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
posted
0
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
posted
0
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.
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
posted
0
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.......
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
posted
0
Hi Raghavan,
Thankyou for a very clear clarification on the difference between string and string buffer . It was of a great help .