##String
首先,String 重写了hashCode 和 equals 方法。
注意:== 和 hashCode 还是有区别的,
==:它代表的是完全相同。
equals:它更多代表的“值相等”。
当然如果没有重写equals方法的话,那么equals就是以==来进行判断的。
下面以String 为例子来分析以下的各种情况:
public class StringTest {
public static void main(String[] args) {
//编译期就可以确定,常量池中没有,就创建该对象存在常量池中
String s1 = "xixi";
//编译期就可以确定,常量池中有,无须创建,指向同一个
String s2 = "xixi";
String s3 = new String("xixi");
String s4 = new String("xixixixi");
//s5无法在编译期就确定下来,所以不能引用常量池对象,其实是用StringBuffer做拼接在用new的方法,是在堆上进行。
String s5 = s1 + s2;
String s6 = "xixixixi";
//可以在编译期确定
final String s7 = "xixi";
String s8 = s7 + "xixi";
System.out.println(s1.hashCode());
System.out.println(s2.hashCode());
System.out.println(s3.hashCode());
System.out.println(s4.hashCode());
System.out.println(s5.hashCode());
System.out.println(s1.equals(s2));
//返回true 是同一个对象
System.out.println(s1 == s2);
System.out.println(s2.equals(s3));
//返回false一个在常量池,一个在堆上是两个对象
System.out.println(s2 == s3);
System.out.println(s4.equals(s5));
//返回false 其实是两个new
System.out.println(s4 == s5);
//返回true 因为final是可以在编译期确定的
System.out.println(s6 == s8);
}
}
结果如下:
下面关于创建几个对象的问题:
1.String a = “123”;
创建了一个或零个对象
jvm在编译阶段会先判断常量池中是否有“123”这个常量对象,如果有,a直接指向这个对象,不创建了。如果没有会在常量池中创建这个对象。
2.String a =new String(“123”);
创建了两个或一个对象
同情况1,jvm编译阶段判断常量池中 “123"存在与否,进而来判断是否创建常量对象,然后运行阶段通过new关键字创建String对象。
3.String a = “123” + “456”;
创建一个或0个对象
jvm编译阶段过编译器优化后会把字符串常量直接合并成"123456”,所有创建对象时最多会在常量池中创建1个对象。
4.String a = “123” + new String(“456”);
最多创建了4个对象
常量池对象"123" ,“456”,new String(“456”)创建堆对象,还有一个堆对象"123456"。