As per my knowledge,
a mutable string can be changed, and an immutable string cannot be changed.
Here I want to change the value of String like this,
String str="Good";
str=str+" Morning";
and other way is,
StringBuffer str= new StringBuffer("Good");
str.append(" Morning");
In both the cases I am trying to alter the value of str
. Can anyone tell me, what is difference in both case and give me clear picture of mutable and immutable objects.
When you say
str
, you should be careful what you mean:do you mean the variable
str
?or do you mean the object referenced by
str
?In your
StringBuffer
example you are not altering the value ofstr
, and in yourString
example you are not altering the state of theString
object.The most poignant way to experience the difference would be something like this:
its very easily explained in this link with setter and getter class is mutable while class with only getter is immutable. Here is the details. http://latest-tutorial.com/2014/11/14/difference-mutable-immutable-objects-java/
A mutable variable is one whose value may change in place, whereas in an immutable variable change of value will not happen in place. Modifying an immutable variable will rebuild the same variable.
immutable exist, mutable don't.
I modified the code of william with a output comments for better understandable
In above code , look at the value of str in both main() and changeStr() , even though u r changing the value of str in changeStr() it is affecting only to that function but in the main function the value is not changed , but it not in the case of StringBuffer..
In StringBuffer changed value is affected as a global..
hence String is immutable and StringBuffer is mutable...
In Simple , whatever u changed to String Object will affecting only to that function By going to String Pool. but not Changed...
In Java, all strings are immutable(Can't change). When you are trying to modify a String, what you are really doing is creating a new one.
Following ways we can create the string object
Using String literal
Strung str="java";
Using new keyword
Strung str = new String("java");
Using character array
char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.' };
String helloString = new String(helloArray);
coming to String immutability, simply means unmodifiable or unchangeable
Let's take one example
I'm initializing the value to the String literal s
Below I'm going to display the decimal representation of the location address using hashcode()
Simply printing the value of a String s
Okay, this time I'm inittializing value "kumar" to s1
Okay let's check by displaying hashcode of the s1 object which we created
okay, let's check below code
Okay, check this below code at last
YES, if you see Strings 's' and 's1' having the same hashcode because the value hold by 's' & 's1' are same that is 'kumar'
Let's consider String 's2' and 's3' these two Strings hashcode appears to be different in the sense, they both stored in a different location because you see their values are different.
since s and s1 hashcode is same because those values are same and storing in the same location.
Example 1: Try below code and analyze line by line
Example 2: Try below code and analyze line by line
Okay, Let's look what is the difference between mutable and immutable.
mutable(it change) vs. immutable (it can't change)
Any further questions?? please write on...