"==" 与"equals() 的区别:
String s1 = "hello";String s2 = "hello";
String s3 = new String("hello");String s4 = new String("hello");
System.out.println("s1.equals(s2) = " + s1.equals(s2));
System.out.println("s1 == s2 ? " + (s1 == s2));
System.out.println("s3.equals(s4) = " + s3.equals(s4));
System.out.println("s1 == s4 ? " + (s3 ==s2));
结果是:s1.equals(s2) = true
s1 == s2 ? true
s3.equals(s4) = true
s1 == s4 ? false
结论:"=="比较的是两个对象的地址,"equals()"比较的是两个对象的值.
String s3 = new String("hello");String s4 = new String("hello");
System.out.println("s1.equals(s2) = " + s1.equals(s2));
System.out.println("s1 == s2 ? " + (s1 == s2));
System.out.println("s3.equals(s4) = " + s3.equals(s4));
System.out.println("s1 == s4 ? " + (s3 ==s2));
结果是:s1.equals(s2) = true
s1 == s2 ? true
s3.equals(s4) = true
s1 == s4 ? false
结论:"=="比较的是两个对象的地址,"equals()"比较的是两个对象的值.