这句话没错吧 If x and y are String references,如果x,y是对象的话,并且x==y,那么x,y一定是同一个对象 例如: String s=new String("abc"); String s1=new String("abc"); System.out.println(s==s1); 打印为false。 理所当然如果是同一个对象那么x.equals(y)就是true的。
显然不正确,在java中==比较的时reference,if x and y refer to exactly the same object(there's only one object), the x==y is true, if x and y refer to two objects with same data fields, then x==y is false,since the address of memory is different besides, if the object has a method equal() to compare to objects, then this method should compare the data fields of the object, not the reference
String对象的equals()比较内容是否相等!
If x and y are String references,如果x,y是对象的话,并且x==y,那么x,y一定是同一个对象
例如:
String s=new String("abc");
String s1=new String("abc");
System.out.println(s==s1);
打印为false。
理所当然如果是同一个对象那么x.equals(y)就是true的。
==运算符是对引用进行比较,如果两个引用指向的是同一个对象的话,结果才是true,既然是指向同一个对象,那么equals方法结果肯定为true的.楼主为什么认为这句话是错的呢?
besides, if the object has a method equal() to compare to objects, then this method should compare the data fields of the object, not the reference