Is the String class in Java really immutable? java interview frequently asked questions

In fact, in Java, the String class is modified by final, mainly to ensure the immutability of the string, thereby ensuring its security. So how does final ensure string security? Next, let us take a look together.

1. The role of final

1. The class modified by the final keyword cannot be inherited by other classes, but the class itself can inherit other classes. In layman's terms, this class can have a parent class, but not a subclass.

1

2

3

final class MyTestClass1 {

    // ...

}

2. Methods modified by the final keyword cannot be overridden, but can be inherited.

1

2

3

4

5

class MyTestClass2 {

    final void myMethod() {

        // ...

    }

}

3. The basic data type modified by the final keyword is called a constant, which can only be assigned once.  

1

2

3

class MyTestClass3 {

    final int number = 100;

}

4. The reference data type variable modified by the final keyword is an address value. The address value cannot be changed, but the data object corresponding to the address can be changed (in fact, this is related to what we are going to talk about today. In Later, I will give you a key explanation based on the case, and everyone must cheer up and study carefully).

5. The member variable modified by the final keyword needs to be assigned before creating the object, otherwise an error will be reported (that is, it needs to be assigned directly at the time of definition).

To sum up, we can know that final is a very useful keyword in Java, which can mainly improve the stability and readability of our code. Of course, the focus of our explanation today is the String class modified by final, so let's turn our attention back to String and see what features String has!

2. The String class modified by final

In order to let everyone better understand the immutability of String, first of all, I would like to briefly talk about the source code design of String. From the source code below, we can figure out a lot of underlying design ideas. Next, please follow me to take a look at the core source code of String.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

/**

 * ......其他略......

 *

 * Strings are constant; their values cannot be changed after they

 * are created. String buffers support mutable strings.

 * Because String objects are immutable they can be shared. For example:

 *

 * ......其他略......

 *

 */

public final class String

    implements java.io.Serializable, Comparable<String>, CharSequence {

     

    ......

Let me give you a simple explanation of the above source code and its comments:

● final: Please refer to the introduction of final features in Section 1;

● Serializable: for serialization;

● Comparable<String>:默认的比较器;

● CharSequence: 提供对字符序列进行统一、只读的操作。

从这段源码及其注释中,我们可以得到下面这些结论:

● String类用final关键字修饰,说明String不可被继承;

● String字符串是常量,字符串的值一旦被创建,就不能被改变;

● String字符串缓冲区支持可变字符串;

● String对象是不可变的,它们是可以被共享的。

三. String的不可变性

在学习了上面的这些核心源码之后,接下来,我们可以通过一个案例来实践验证一番,看看String字符串的内容到底能不能改变。这里有个代码案例,如下图所示:

在上述的案例结果中,大家可以看出,s的内容竟然发生了改变?!但我们不是一直说String是不可变的吗?这是咋回事?大家先别急,我们继续往下看。

要想弄明白这个问题,我们首先得知道一个知识点:引用和值的区别!

在上面的代码中,我们先是创建了一个 "yiyige" 为内容的字符串引用s,如下图:

s其实先是指向了value对象,而value对象又指向了存储 "y,i,y,i,g,e" 字符的字符数组。但因为value被final修饰,所以value的值不可被更改。因此,上面代码中改变的其实是s的引用指向,而不是改变了String对象的值!

换句话说,上面实例中s的值,其实只是value的引用地址,并不是String的内容本身。当我们执行 s = "yyg" 语句时,Java会创建一个新的字面量对象 "yyg",而原来的 "yiyige" 字面量对象其实依然存在于内存的intern缓存池中。

在这里,String对象的改变,实际上是通过内存地址的“断开-连接”变化来完成的。在这个过程中,原字符串中的内容并没有发生任何的改变。String s = "yiyige" 和 s = "yyg"这两行代码,实质上是开辟了2个内存空间,s只是由原来指向 "yiyige" 变为指向 "yyg" 而已,而其原来的字符串内容,是没有发生改变的,如下图所示。

因此,我们在以后的开发中,如果要经常修改字符串的内容,请尽量少用String!因为如果字符串的指向经常的“断开-连接”,就会大大降低性能,我建议大家使用StringBuilder 或 StringBuffer 进行替换。

我们继续把上面的代码深入地分析一下。在Java中,因为数组也是对象, 所以value中存储的也只是一个引用,它指向一个真正的数组对象。在执行了String s = “yiyige”; 这句代码之后,真正的内存布局应该是下图这样的:

因为value是String封装的字符数组,value中所有的字符都属于String这个对象。而由于value是private的,没有提供setValue等公共方法来修改这个value值,所以我们在String类的外部是无法修改value值的,也就是说字符串一旦初始化就不能再被修改。

此外,value变量是final修饰的,也就是说在String类内部,一旦这个值初始化了,value这个变量所引用的地址就不会改变了,即一直引用同一个对象。正是基于这一层,我们才说String对象是不可变的对象。

所以String的不可变,其实是指value在栈中的引用地址不可变,而不是说常量池中value字符数组里的数据元素不可变。也就是说,value所引用的数组对象里的内容,其实是可以发生改变的。

那么我们又如何改变它呢?这就要通过反射来消除String类对象的不可变性啦!

四. String真的不可变吗?

在上述内容中,我们重点给大家解释了String字符串的可变性。现在大家应该已经知道了,String字符串的内容其实是可变的,不可改变的只是String字符串的对象地址。那么我们到底该怎么让String字符串的内容发生改变呢?在上述我们给大家提到了反射,接下来我们就来看看如何通过反射改变String字符串的内容吧。代码案例如下所示:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

try {

    String str = "yyg";

    System.out.println("str=" + str + ", 唯一性hash值=" + System.identityHashCode(str));

    Class stringClass = str.getClass();

    //获取String类中的value属性

    Field field = stringClass.getDeclaredField("value");

    //设置私有成员的可访问性,进行暴力反射

    field.setAccessible(true);

    //获取value数组中的内容

    char[] value = (char[]) field.get(str);

    System.out.println("value=" + Arrays.toString(value));

    value[1] = 'z';

    System.out.println("str=" + str + ", 唯一性hash值=" + System.identityHashCode(str));

catch (NoSuchFieldException | IllegalAccessException e) {

    e.printStackTrace();

}

执行结果如下图所示:

从上面的结果中我们可以看到,String字符串的字符数组,通过反射进行修改后,字符串的“内容”真的发生了变化!

并且我们又利用底层的java.lang.System#identityHashCode()方法(不管是否重写了hashCode方法),来获取到了该字符串对象的唯一哈希值,该方法获取的hash值与hashCode()方法是一样的。

从结果中,我们可以看到两个字符串的唯一hash值是一样的,这就证明字符串的引用地址没有发生改变。

所以这就说明,我们并不是像之前那样创建了一个新的String字符串,而是真的改变了原有String的内容。

这个代码案例进一步证明了我们上面的结论:String字符串的不可变,指的其实是value对象在栈中的引用地址不可变,而不是说常量池中value里的数据元素不可变!简单地说,就是String字符串的内容其实是可以改变的,不能改表的是它的对象地址而已。

所以这也就是我们上述所说的final的作用之一:final关键词修饰的引用数据类型的变量,其值为地址值,地址值不能改变,但是地址内的数据对象可以被改变!

五. 总结

至此,我们就把今天的面试题分析完了,现在你明白了吗?最后我再来给大家总结一下今天的重点内容吧:

1.  为什么要用final修饰java中的String类呢?

核心:因为它确保了字符串的安全性和可靠性。

2.  java中的String真的不可变吗?

核心:String字符串的内容其实是可变的,但要通过特殊手段进行实现,不可改变的是String字符串对象的地址。

3.  如何消除String类对象的不可变性?

核心:利用反射来消除String类对象的不可变性。

4.  如果想要保证String的不可变要注意哪些?

● 首先,将 String 类声明为 final类型。这意味着String类是不可被继承的,防止程序员通过继承重写String类的某些方法,使得String类出现“可变的”的情况;

● 然后,重要的字符数组value属性,要被private 和 final修饰。它是String的底层数组,用于存贮字符串内容。又因为数组是引用类型,所以只能限制引用不被改变,也就是说数组元素的值是可以改变的,这在上面的案例中已经证明过了;

● 接着,所有修改的方法都返回新的字符串对象,保证修改时不会改变原始对象的引用;

● 最后,不同的字符串对象都可以指向缓存池中的同一个字符串字面量。

当然Java中的String类使用final修饰”这个概念非常重要,因为它确保了字符串的安全性和可靠性。但是我们也要清楚不可改变的只是它的地址,而不是它的内容,它的内容是可以利用反射来改变的!只不过在一般的描述中,大家都会说String内容不可改变,毕竟很多时候是不允许利用反射这种特殊的功能去进行这样的操作的。

来源:Java中的String类真的不可变吗?java面试常见问题 - 可爱的小锋 - 博客园 (cnblogs.com)

Guess you like

Origin blog.csdn.net/wangonik_l/article/details/131439152