String s1 = "helloworld";
String s2 = "helloworld";
String s3 = new String(s1);//"helloworld"
String s4 = new String(s2);//"helloworld"
String s5 = new String("helloshanghai");
String s6 = new String("helloshanghai");
System.out.println("s1.hascode :"+s1.hashCode());
System.out.println("s2.hascode :"+s2.hashCode());
System.out.println("s3.hascode :"+s3.hashCode());
System.out.println("s4.hascode :"+s4.hashCode());
System.out.println("s5.hascode :"+s5.hashCode());
System.out.println("s6.hascode :"+s6.hashCode());
System.out.println("---------------------------------");
System.out.println("s1 == s2 :"+(s1 == s2));
System.out.println("s1.equals(s2) :"+(s1.equals(s2)));
System.out.println("s3 == s4 :"+(s3 == s4));
System.out.println("s3.equals(s4) :"+(s3.equals(s4)));
System.out.println("s1 == s3 :"+(s1 == s3));
System.out.println("s1.equals(s3) :"+(s1.equals(s3)));
这个==和.equals()有点问题,一直没明白他们的本质区别,还是被混淆了,求一份详细的解答。还有为啥String类new一个新的对象时候,他们的hashcode还是一样,不是分配的内存不是同一块吗? |