灵魂拷问:java的String到底可不可变?

ゞ 浴缸里的玫瑰 2023-08-17 16:25 244阅读 0赞

从认识java的那天起,就被告知String是不可变的,因为源码上是这样写的

  1. public final class String
  2. implements java.io.Serializable, Comparable<String>, CharSequence {
  3. /** The value is used for character storage. */
  4. private final char value[];
  5. /** Cache the hash code for the string */
  6. private int hash; // Default to 0
  7. /** use serialVersionUID from JDK 1.0.2 for interoperability */
  8. private static final long serialVersionUID = -6849794470754667710L;
  9. ……
  10. ……
  11. ……

很好理解,因为被final关键字修饰了,所以是不可变的,但你能清楚的解释下面的问题吗

  1. public static void main(String[] args) {
  2. String a = new String("abcd");
  3. String b = new String("abcd");
  4. String c = "abcd" + "ppp";
  5. String d = "abcd";
  6. String e = "abcd" + "ppp";
  7. String f = d + "ppp";
  8. System.out.println("情况1:"+(a == b) + "-------------" + a.equals(b));
  9. System.out.println("情况2:"+(a == d) + "-------------" + a.equals(d));
  10. System.out.println("情况4:"+(c == e) + "-------------" + c.equals(e));
  11. System.out.println("情况5:"+(f == e) + "-------------" + f.equals(e));
  12. a = a.intern();
  13. System.out.println("情况6:"+(a == d) + "-------------" + a.equals(d));
  14. }

是不是疯了?

其实每种情况的后半段equals好理解,根据源码的描述

  1. public boolean equals(Object anObject) {
  2. if (this == anObject) {
  3. return true;
  4. }
  5. if (anObject instanceof String) {
  6. String anotherString = (String)anObject;
  7. int n = value.length;
  8. if (n == anotherString.value.length) {
  9. char v1[] = value;
  10. char v2[] = anotherString.value;
  11. int i = 0;
  12. while (n-- != 0) {
  13. if (v1[i] != v2[i])
  14. return false;
  15. i++;
  16. }
  17. return true;
  18. }
  19. }
  20. return false;
  21. }

看上面的源码知道String重写的Object的eqals方法,本质是转成字符数组然后逐一比较,所以上述6种情况后半段都是true

一个个来分析,情况1

  1. String a = new String("abcd");
  2. String b = new String("abcd");
  3. System.out.println("情况1:"+(a == b) + "-------------" + a.equals(b));

看一下带参数的构造函数的源码:

  1. /**
  2. * Initializes a newly created {@code String} object so that it represents
  3. * the same sequence of characters as the argument; in other words, the
  4. * newly created string is a copy of the argument string. Unless an
  5. * explicit copy of {@code original} is needed, use of this constructor is
  6. * unnecessary since Strings are immutable.
  7. *
  8. * @param original
  9. * A {@code String}
  10. */
  11. public String(String original) {
  12. this.value = original.value;
  13. this.hash = original.hash;
  14. }

首先this.value是String定义的一个私有的被final修饰成员变量private final char value[];,将原始字符串的value和hash值进行赋值后,会返回一个String对象的引用,所以a和b返回的是两个引用,自然不一样,所以情况1前半段为false

情况2

  1. String a = new String("abcd");
  2. String d = "abcd";
  3. System.out.println("情况2:"+(a == d) + "-------------" + a.equals(d));

看着好像是一样的,但这个就涉及到String字符池的概念,其实在情况1中,jvm会在内部维护的String Pooll中放入一个”abc”对象,并在heap中创建一个String对象,然后将该heap中对象的引用返回给用户
大概意思如下图
在这里插入图片描述
堆内存和字符串有什么不同呢,堆内存会随着对象的回收而被GC回收,而字符池不会

所以执行String d = "abcd";的时候,实际上java的处理方式是先去字符池找,有没有已经存在的字符串,如果有,则返回一个指向它的引用,如果没有,则与new String(“abcd”)处理一样,这里可知,字符池中已经有,所以返回的只是一个String类型的引用,而===比较的就是引用,所以情况2的前半段结果也是false

情况3

  1. String b = new String("abcd");
  2. System.out.println("情况3:"+(b == d) + "-------------" + b.equals(d));
  3. String d = "abcd";

与情况2如出一辙,前半段结果也是false

情况4

  1. String c = "abcd" + "ppp";
  2. String e = "abcd" + "ppp";
  3. System.out.println("情况4:"+(c == e) + "-------------" + c.equals(e));

如果用+号来实现String的串接时:1)仅当+号两边均为字符串常量时,才将其+后的结果当做字符串常量,且该结果直接放入String Pool;2)若+号两边有一方为变量时,+后的结果即当做非字符串常量处理(等同于new String()的效果) ,所以可知c和e引用的是用一个对象,所以情况4的前半段结果是true

情况5

  1. String d = "abcd";
  2. String e = "abcd" + "ppp";
  3. String f = d + "ppp";
  4. System.out.println("情况5:"+(f == e) + "-------------" + f.equals(e));

根据情况4的分析,+号有一方为变量,处理与new String()一致,自然前半段结果也是false

情况6

  1. String a = new String("abcd");
  2. a = a.intern();
  3. System.out.println("情况6:"+(a == d) + "-------------" + a.equals(d));

情况6与情况2唯一的不同就是a = a.intern();,那这个a = a.intern();到底做了什么呢,源码是这样说的

  1. /**
  2. * Returns a canonical representation for the string object.
  3. * <p>
  4. * A pool of strings, initially empty, is maintained privately by the
  5. * class {@code String}.
  6. * <p>
  7. * When the intern method is invoked, if the pool already contains a
  8. * string equal to this {@code String} object as determined by
  9. * the {@link #equals(Object)} method, then the string from the pool is
  10. * returned. Otherwise, this {@code String} object is added to the
  11. * pool and a reference to this {@code String} object is returned.
  12. * <p>
  13. * It follows that for any two strings {@code s} and {@code t},
  14. * {@code s.intern() == t.intern()} is {@code true}
  15. * if and only if {@code s.equals(t)} is {@code true}.
  16. * <p>
  17. * All literal strings and string-valued constant expressions are
  18. * interned. String literals are defined in section 3.10.5 of the
  19. * <cite>The Java™ Language Specification</cite>.
  20. *
  21. * @return a string that has the same contents as this string, but is
  22. * guaranteed to be from a pool of unique strings.
  23. */
  24. public native String intern();

大概意思就是如果常量池中存在当前字符串, 就会直接返回当前字符串. 如果常量池中没有此字符串, 会将此字符串放入常量池中后, 再返回

所以a.intern()是显式的调用了intern()方法,将a的引用由原来的指向heap中对象改为指向内部维护的strings pool中的对像,a原来指向的String对象已经成为垃圾对象了,随时会被GC收集
如此一来自然前半段结果就是true了,真TM神奇

总结一下,String对象真的是不可变的,“可变的”是引用,jvm通过上面的策略,可以使多个引用指向一个对象而互不影响。字符池的存在当然是基于节约内存考虑。

最后我们来看一下结果
在这里插入图片描述
好像是全对了,100分,但是故事结束了吗?

  1. public static String changeStr(String before) throws Exception{
  2. System.out.println("之前是这个---- "+before); //
  3. Field field = String.class.getDeclaredField("value");
  4. field.setAccessible(true);
  5. char[] value = (char[]) field.get(before);
  6. value[6] = 'J';
  7. value[7] = 'a';
  8. value[8] = 'v';
  9. value[9] = 'a';
  10. value[10] = '!';
  11. value[11] = '!';
  12. System.out.println("之后呀是这个---- "+before);
  13. return before;
  14. }
  15. public static void main(String[] args) {
  16. try {
  17. System.out.println(changeStr("Hello String"));
  18. }catch (Exception e ){
  19. // 异常处理
  20. }
  21. }

在这里插入图片描述
啪,打脸,好疼!!!

发表评论

表情:
评论列表 (有 0 条评论,244人围观)

还没有评论,来说两句吧...

相关阅读