String很另类
String字符串太重要,以至于Java对它特别对待。
String s1 = “ABC”; Java是一个面向对象的语言,基本类型用=直接赋值字面量,创建一个对象都是new Xxx(),那么String是基本类型?很明显不是。
声明一个字符串发生了什么
从编译到运行
public static void main(String[] args) {
String s1 = "ABC";
String s2 = "A" + "BC";
String s3 = new String("A" + "BC");
System.out.println(s1 == s2); // true
System.out.println(s1 == s3); // false
}
- 为什么s1 == s2?
首先字符串是字节编译到class的,“ABC"和"A”+"BC"在字节码上是无差别的,都指向了23#常量。字符串不可变意味着可共享,s1已经声明了一个字符串对象,s2会直接去常量池找有没有相同字符串,有用同一个地址,而这个地址指向堆的char[ ]数组。
堆栈有两种引用:间接和直接引用
- 为什么s1 != s3?
按照上面的推理,s1应该等于s3。不同的是,只要new就势必创建一块内存空间,s3不在指向常量池对象,而是一个普通对象,有趣的是这个普通对象指向了同一个char[ ]。
证明上述观点
/** The value is used for character storage. */
private final char value[];
/** Cache the hash code for the string */
private int hash; // Default to 0
String类有其中有一个由final修饰的实例域value,通过反射拿到s1、s2和s3对象的value,看看在内存中是以什么形式存在。
public static void main(String[] args) throws Exception {
String s1 = "ABC";
String s3= new String("A"+"BC");
System.out.println(s1==s3);
Class cls = String.class;
Field fld=cls.getDeclaredField("value");
fld.setAccessible(true);
char[] chs1 = (char[])fld.get(s1);
char[] chs2 = (char[])fld.get(s3);
System.out.println(chs1==chs2);
chs2[2]='六';
System.out.println(s1);
System.out.println(s3);
}
结论: 网上说创建一个的两个的什么的都有,最后无聊的我证实了一下就是上图的样子!