string内存分析

string内存分析

String s = "abc";

String s1 = "abc";

System.out.println(s== s1);//true:abc可以共享

System.out.println(s.equals(s1));//true

String s2 = new String("abc");//在堆内存开辟空间

String s3 = new String("abc");    

System.out.println(s2== s3);//false,两个对象在堆内存中的地址不一样。

System.out.println(s2.equals(s3));//true。虽然堆内存地址不一样,但是他们指向常量池中的同一字符串常量”abc”,值相同。