What does immutable mean?

后端 未结 9 731
借酒劲吻你
借酒劲吻你 2020-11-29 16:07

If a string is immutable, does that mean that.... (let\'s assume JavaScript)

var str = \'foo\';

alert(str.substr(1)); // oo

alert(str); // foo
9条回答
  •  情歌与酒
    2020-11-29 16:45

    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)

提交回复
热议问题