Do you really understand the immutability of Java strings?

1、 Background

The immutability of strings can be said to be a common "simple" problem in interviews.

Common answers are as follows:

The string cannot be changed after creation.

The immutability of strings means that the characters of strings are immutable.

The value character array of String is declared as final to ensure immutable.

Is that true?

Let's consider two more questions:

So what exactly does immutability of strings mean?

How is it guaranteed?

Let's look at a strange phenomenon: the following statement printed "aw" at the end of a program. Why?

//The preceding code is omitted


I suggest you think first and then look at the following.

2、 Cases

What do you think is the output of the following example code?

Is it different from what some students think?

The character array of a string can be modified by reflection, resulting in changes in the "content" of the string.

Native method, which gives the unique hash value of the object (regardless of whether the hashCode method is overridden).

The object has not changed.

So, we know that the hash value of String is calculated from the character array of string (JDK8). Why is the return value of the hashCode function the same twice?

Let's take a closer look at the java. lang. String # hashCode source code

It is found that after the first call to the hashCode function, the calculation result of the hashCode is cached in the string object through the hash attribute (as long as it is cached, it will not be recalculated), so the second time is the same as the first time.

So how to guarantee the variability?

First, declare String as fianl to ensure that it is not inheritable.

Then, all modified methods return a new string object to ensure that the reference of the original object will not be changed during modification.

If it exists in the object pool, it directly points to the string object in the object pool, otherwise it creates a string object and puts it in the object pool and points to the object.

Therefore, we can see that the immutability of strings refers to the immutability of references.

Although the value character array in String is declared as final, this final is only to make the reference of value immutable, not to make the characters of the character array irreplaceable.

Because the beginning ab and the last ab are literal and point to the same object in the same string pool, the property modification of the object and the printing in both places will be affected.

3、 Thinking

Many simple questions are not as simple as they seem.

When you read the source code of the technology blog, you must have your own thinking, ask a few more why, and have the opportunity to practice more.

When you learn a technology, you should develop essential thinking, that is, thinking about the essence of the problem.

During the interview, the simple questions should be answered comprehensively and in depth, and the questions that can't be answered should have their own ideas, so that there will be more opportunities.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us