文章目录
一、两个String对象,hashcode相同,那么两个String一定相同吗?
答:不是。
测试代码
public class StringEqualTest {
public static void main(String[] args) {
String a = "ABCDEa123abc";
String b = "ABCDFB123abc";
System.out.println(a.hashCode());//165374702
System.out.println(b.hashCode());//165374702
System.out.println(a.hashCode() == b.hashCode());//true
System.out.println(a.equals(b));//false
}
}
结果
165374702
165374702
true
false
二、String a = “123”;和String a = new String(“123”);有什么区别?
答:String s = new String(“hello”)会创建2(1)个对象,String s = “hello”创建1(0)个对象。
注:当字符串常量池中有对象hello时括号内成立!
测试代码1
public class StringDemo2 {
public static void main(String[] args) {
String s1 = new String("hello");
String s2 = "hello";
System.out.println(s1 == s2);// false
System.out.println(s1.equals(s2));// true
}
}
**运行结果:**
> false
> true
前提知识
==与equals()的区别:
==:比较引用类型比较的是地址值是否相同
equals:比较引用类型默认也是比较地址值是否相同,而String类重写了equals()方法,比较的是内容是否相同。
内存图
下面这个是JDK1.6的图了,1.7及以后版本字符串常量池放到了堆中。
JVM内存结构图:
测试代码1详解
首先,通过main()方法进栈。
然后再栈中定义一个对象s1,去堆中开辟一个内存空间,将内存空间的引用赋值给s1,“hello”是常量,然后去字符串常量池 查看是否有hello字符串对象,没有的话分配一个空间存放hello,并且将其空间地址存入堆中new出来的空间中。
在栈中定义一个对象s2,然后去字符串常量池中查看是否有”hello”字符串对象,有,直接把”hello”的地址赋值给s2.
即s1中存的是堆中分配的空间,堆中分配的空间中存的是字符串常量池中分配空间存放”hello”的空间的地址值。而s2中之间存的是字符串常量池中分配空间存放”hello”的空间的地址值。
由于s1与s2中存放的地址不同,所以输出false。因为,类String重写了equals()方法,它比较的是引用类型的 的值是否相等,所以输出true。即结果为false、true。
测试代码2
public class StringDemo1 {
public static void main(String[] args) {
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1 == s2);// false
System.out.println(s1.equals(s2));// true
String s3 = new String("hello");
String s4 = "hello";
System.out.println(s3 == s4);// false
System.out.println(s3.equals(s4));// true
String s5 = "hello";
String s6 = new String("hello");
System.out.println(s5 == s6);// false
System.out.println(s5.equals(s6));// true
String s7 = "hello";
String s8 = "hello";
System.out.println(s7 == s8);// true
System.out.println(s7.equals(s8));// true
}
}
测试代码2详解
s1~s6用equals()的比较不解释,都是比较的值,均为true。以下讲解==
s1、s2:二者均为new出来的,各自在堆中分配有空间,并各自将内存地址赋值给s1、s2。空间地址不同,==比较为false。但是各自在堆中空间中保存的值均为在字符串常量池中的同一个对象的地址。根据Demo处的图即解释不难理解。
s3、s4,s5、s6同上Demo出解释。
s7、s8都是在常量池中取值,二者都指向常量池中同一对象,其地址值相同,所以结果为true。
测试代码3
public class StringDemo4 {
public static void main(String[] args) {
String s1 = "hello";
String s2 = "world";
String s3 = "helloworld";
System.out.println(s3 == s1 + s2);// false
System.out.println(s3.equals((s1 + s2)));// true
System.out.println(s3 == "hello" + "world");//true
System.out.println(s3.equals("hello" + "world"));// true
String s4 = "hello";
String s5 = new String("hello");
String s6 = "world";
String s7 = new String("world");
String s8 = "helloworld";
String s9 = new String("helloworld");
System.out.println(s8 == s4 + s7);//false
System.out.println(s8 == s5 + s7);//false
System.out.println(s8 == "hello" + s6);//false
System.out.println(s8 == s4 + "world");//false
System.out.println(s8 == "hello" + new String("world"));//false
System.out.println(s8 == new String("hello") + "world");//false
}
}
测试代码3详解
equals()比较方法不解释,比较值,均相等,均为true。
s1与s2相加是先在字符串常量池中开一个空间,然后拼接,这个空间的地址就是s1与s2拼接后的地址。与s3的地址不同,所以输出为false。
s3与”hello”+”world”作比较,”hello”+”world”先拼接成”helloworld”,然后再去字符串常量池中找是否有”helloworld”,有,所以和s3共用一个字符串对象,则为true。
总结
String s = new String(“hello”)会创建2(1)个对象,String s = “hello”创建1(0)个对象。
注:当字符串常量池中有对象hello时括号内成立!
字符串如果是变量相加,先开空间,在拼接。
字符串如果是常量相加,是先加,然后在常量池找,如果有就直接返回,否则,就创建。
问题2非常感谢博文https://www.cnblogs.com/taochen-go/p/9475947.html中的内容作参考,所有权利归原作者所有,如有不妥,可联系我删除。