从认识java的那天起,就被告知String是不可变的,因为源码上是这样写的
public final class String
implements java.io.Serializable, Comparable, CharSequence {
/** The value is used for character storage. */
private final char value[];
/** Cache the hash code for the string */
private int hash; // Default to 0
/** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = -6849794470754667710L;
……
……
……
很好理解,因为被final关键字修饰了,所以是不可变的,但你能清楚的解释下面的问题吗
public static void main(String[] args) {
String a = new String("abcd");
String b = new String("abcd");
String c = "abcd" + "ppp";
String d = "abcd";
String e = "abcd" + "ppp";
String f = d + "ppp";
System.out.println("情况1:"+(a == b) + "-------------" + a.equals(b));
System.out.println("情况2:"+(a == d) + "-------------" + a.equals(d));
System.out.println("情况4:"+(c == e) + "-------------" + c.equals(e));
System.out.println("情况5:"+(f == e) + "-------------" + f.equals(e));
a = a.intern();
System.out.println("情况6:"+(a == d) + "-------------" + a.equals(d));
}
是不是疯了?
其实每种情况的后半段equals
好理解,根据源码的描述
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
看上面的源码知道String重写的Object的eqals方法,本质是转成字符数组然后逐一比较,所以上述6种情况后半段都是true
一个个来分析,情况1
String a = new String("abcd");
String b = new String("abcd");
System.out.println("情况1:"+(a == b) + "-------------" + a.equals(b));
看一下带参数的构造函数的源码:
/**
* Initializes a newly created {@code String} object so that it represents
* the same sequence of characters as the argument; in other words, the
* newly created string is a copy of the argument string. Unless an
* explicit copy of {@code original} is needed, use of this constructor is
* unnecessary since Strings are immutable.
*
* @param original
* A {@code String}
*/
public String(String original) {
this.value = original.value;
this.hash = original.hash;
}
首先this.value
是String定义的一个私有的被final
修饰成员变量private final char value[];
,将原始字符串的value和hash值进行赋值后,会返回一个String对象的引用,所以a和b返回的是两个引用,自然不一样,所以情况1前半段为false
情况2
String a = new String("abcd");
String d = "abcd";
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
String b = new String("abcd");
System.out.println("情况3:"+(b == d) + "-------------" + b.equals(d));
String d = "abcd";
与情况2如出一辙,前半段结果也是false
情况4
String c = "abcd" + "ppp";
String e = "abcd" + "ppp";
System.out.println("情况4:"+(c == e) + "-------------" + c.equals(e));
如果用+号来实现String的串接时:1)仅当+号两边均为字符串常量时,才将其+后的结果当做字符串常量,且该结果直接放入String Pool;2)若+号两边有一方为变量时,+后的结果即当做非字符串常量处理(等同于new String()的效果) ,所以可知c和e引用的是用一个对象,所以情况4的前半段结果是true
情况5
String d = "abcd";
String e = "abcd" + "ppp";
String f = d + "ppp";
System.out.println("情况5:"+(f == e) + "-------------" + f.equals(e));
根据情况4的分析,+号有一方为变量,处理与new String()
一致,自然前半段结果也是false
情况6
String a = new String("abcd");
a = a.intern();
System.out.println("情况6:"+(a == d) + "-------------" + a.equals(d));
情况6与情况2唯一的不同就是a = a.intern();
,那这个a = a.intern();
到底做了什么呢,源码是这样说的
/**
* Returns a canonical representation for the string object.
*
* A pool of strings, initially empty, is maintained privately by the
* class {@code String}.
*
* When the intern method is invoked, if the pool already contains a
* string equal to this {@code String} object as determined by
* the {@link #equals(Object)} method, then the string from the pool is
* returned. Otherwise, this {@code String} object is added to the
* pool and a reference to this {@code String} object is returned.
*
* It follows that for any two strings {@code s} and {@code t},
* {@code s.intern() == t.intern()} is {@code true}
* if and only if {@code s.equals(t)} is {@code true}.
*
* All literal strings and string-valued constant expressions are
* interned. String literals are defined in section 3.10.5 of the
* The Java™ Language Specification.
*
* @return a string that has the same contents as this string, but is
* guaranteed to be from a pool of unique strings.
*/
public native String intern();
大概意思就是如果常量池中存在当前字符串, 就会直接返回当前字符串. 如果常量池中没有此字符串, 会将此字符串放入常量池中后, 再返回
所以a.intern()
是显式的调用了intern()
方法,将a的引用由原来的指向heap中对象改为指向内部维护的strings pool中的对像,a原来指向的String对象已经成为垃圾对象了,随时会被GC收集
如此一来自然前半段结果就是true
了,真TM神奇
总结一下,String对象真的是不可变的,“可变的”是引用,jvm通过上面的策略,可以使多个引用指向一个对象而互不影响。字符池的存在当然是基于节约内存考虑。
最后我们来看一下结果
好像是全对了,100分,但是故事结束了吗?
public static String changeStr(String before) throws Exception{
System.out.println("之前是这个---- "+before); //
Field field = String.class.getDeclaredField("value");
field.setAccessible(true);
char[] value = (char[]) field.get(before);
value[6] = 'J';
value[7] = 'a';
value[8] = 'v';
value[9] = 'a';
value[10] = '!';
value[11] = '!';
System.out.println("之后呀是这个---- "+before);
return before;
}
public static void main(String[] args) {
try {
System.out.println(changeStr("Hello String"));
}catch (Exception e ){
// 异常处理
}
}