What does immutable mean?
If a string is immutable, does that mean that….
var str = ‘foo’;
alert(str.substr(1)); // oo
alert(str); // foo
Does it mean, when calling methods on a string, it will return the modified string, but it won’t change the initial string?
If the string was mutable, does that mean the 2nd alert() would return oo as well?
It means that once you instantiate the object, you can’t change its properties. In your first alert you aren’t changing foo. You’re creating a new string. This is why in your second alert it will show “foo” instead of oo.
Does it mean, when calling methods on
a string, it will return the modified
string, but it won’t change the
Yes. Nothing can change the string once it is created. Now this doesn’t mean that you can’t assign a new string object to the
str variable. You just can’t change the current object that str references.
If the string was mutable, does that
mean the 2nd alert() would return oo
Technically, no, because the substring method returns a new string. Making an object mutable, wouldn’t change the method. Making it mutable means that technically, you could make it so that substring would change the original string instead of creating a new one.
On a lower level, immutability means that the memory the string is stored in will not be modified. Once you create a string
"foo", some memory is allocated to store the value
"foo". This memory will not be altered. If you modify the string with, say,
substr(1), a new string is created and a different part of memory is allocated which will store
"oo". Now you have two strings in memory,
"oo". Even if you’re not going to use
"foo" anymore, it’ll stick around until it’s garbage collected.
One reason why string operations are comparatively expensive.
Immutable means that which cannot be changed or modified.
So when you assign a value to a string, this value is created from scratch as opposed to being replaced. So everytime a new value is assigned to the same string, a copy is created. So in reality, you are never changing the original value.
"foo") or with a
String class constructor. Strings constructed with string literals are a part of the String Constant Pool, and the same string literal will always be the same memory address from the pool.
String lit1 = "foo"; String lit2 = "foo"; String cons = new String("foo"); System.out.println(lit1 == lit2); // true System.out.println(lit1 == cons); // false System.out.println(lit1.equals(cons)); // true
In the above, both
lit2 are constructed using the same string literal, so they’re pointing at the same memory address;
lit1 == lit2 results in
true, because they are exactly the same object.
cons is constructed using the class constructor. Although the parameter is the same string constant, the constructor allocates new memory for
cons is not the same object as
lit2, despite containing the same data.
Of course, since the three strings all contain the same character data, using the
equals method will return true.
(Both types of string construction are immutable, of course)
Immutable means the value can not be changed. Once created a string object can not be modified as its immutable. If you request a substring of a string a new String with the requested part is created.
Using StringBuffer while manipulating Strings instead makes the operation more efficient as StringBuffer stores the string in a character array with variables to hold the capacity of the character array and the length of the array(String in a char array form)
The text-book definition of mutability is liable or subject to change or alteration.
In programming, we use the word to mean objects whose state is allowed to change over time. An immutable value is the exact opposite – after it has been created, it can never change.
If this seems strange, allow me to remind you that many of the values we use all the time are in fact immutable.
var statement = "I am an immutable value"; var otherStr = statement.slice(8, 17);
I think no one will be surprised to learn that the second line in no way changes the string in statement.
In fact, no string methods change the string they operate on, they all return new strings. The reason is that strings are immutable – they cannot change, we can only ever make new strings.