Why String is immutable?

Foreword

Why in java String is immutable?
Why if the String immutable? What is the significance?

The question: Why in java String is immutable?

Beginners will meet some doubts,

"Everybody says String immutable, but I obviously can change a String ah"

Yes, as in the following

public class test {
    public static void main(String[] args) {
        String oldStr = "阳光大男孩!!!";
        System.out.println(oldStr);
        oldStr = oldStr.substring(3);
        System.out.println(oldStr);
    }
}

The output is

Sunshine Boys! ! !
boy! ! !

You see, this is not able to change it? Bloggers spicy chicken!

In fact, this is a misunderstanding to understand the immutability of String

This code is actually such a process

  1. A string of new applications, content for the "Sunshine Boys !!!"
  2. The value of the output string
  3. Capture operation performed on the string, built a String object! ! ! , Let oldStr point to this newly created object.
  4. Print oldStr, this time oldStr points to the new object , the value is "boy !!!"

Evidence about

We look at open source substring method

public String substring(int beginIndex) {
        if (beginIndex < 0) {
            throw new StringIndexOutOfBoundsException(beginIndex);
        }
        int subLen = value.length - beginIndex;
        if (subLen < 0) {
            throw new StringIndexOutOfBoundsException(subLen);
        }
        return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
    }

The last line was a three expressions, if beginIndex is 0, then, do not need to explain the interception, so a direct return to the original object, otherwise, create a new object .

So, you change the value of the string on the surface, it did not, the original string has not changed, but a new new object.

Question two: Why String If immutable? What is the significance?

  • Is to achieve a string array, the array is stored sequentially, it created when it opened up a continuous address
  • There are multiple threads need to share variables, String immutability, so that the program becomes safe because immutable means not only write read, it is safe.
  • Design string constant pool can improve code efficiency, space saving, and immutability of String exactly a prerequisite.

About string constant pool analysis

public class test {
    public static void main(String[] args) {
        String oldStr1 = "阳光大男孩!!!";
        String oldStr2 = "阳光大男孩!!!";
        System.out.println(oldStr1.hashCode());
        System.out.println(oldStr2.hashCode());
    }
}

Output at

1402535102
1402535102

We can see that the two object references are the same address,

  • In the first object is created when, after created, and will put it in a string constant pool.
  • new second object when the string constants will go to the pool, and if there is, then let instance directly to the address of the constant pool, otherwise new a new object.
Published 200 original articles · won praise 99 · views 40000 +

Guess you like

Origin blog.csdn.net/weixin_43889841/article/details/103413899