Which of the following statements are true?
A. The equals() method determines if reference values refer to the same object.B. The == operator determines if the contents and type of two separate objects match.C. The equals() method returns true only when the contents of two objects match.D. The class File overrides equals() to return true if the contents and type of two separate objects match.
翻译
下面的哪些叙述为真。A. equals()方法判定引用值是否指向同一对象。B. == 操作符判定两个分立的对象的内容和类型是否一致。C. equals()方法只有在两个对象的内容一致时返回true。D. 类File重写方法equals()在两个分立的对象的内容和类型一致时返回true。
答案A,D
我觉得答案错了,应该是C D
"equals"比较的是传递的实际值,且可以将reference和object混合比较,
而"=="只能同级比较,比较的是当前句柄指向的低一级的句柄是否相同,且不可以将reference和object混合比较请各位发个言
A. The equals() method determines if reference values refer to the same object.B. The == operator determines if the contents and type of two separate objects match.C. The equals() method returns true only when the contents of two objects match.D. The class File overrides equals() to return true if the contents and type of two separate objects match.
翻译
下面的哪些叙述为真。A. equals()方法判定引用值是否指向同一对象。B. == 操作符判定两个分立的对象的内容和类型是否一致。C. equals()方法只有在两个对象的内容一致时返回true。D. 类File重写方法equals()在两个分立的对象的内容和类型一致时返回true。
答案A,D
我觉得答案错了,应该是C D
"equals"比较的是传递的实际值,且可以将reference和object混合比较,
而"=="只能同级比较,比较的是当前句柄指向的低一级的句柄是否相同,且不可以将reference和object混合比较请各位发个言
C 感觉也不对,两个不同的对象内容相对,使用equals()返回的也是false
String s2=new String(“JDK1.2”);
方法s1.equals(s2)返回真,尽管s1和s2指向两个不同的对象。
是两个不同类型的对象内容相同时,用equals()返回的是false
对A我很迷惑,就像edc(乱七八糟) 举的例子,s1,s2指向不同对象,只要他们是同类型并且内容相同,就返回true。但是在java doc 里面有一句原话“this method returns true if and only if x and y refer to the same object (x == y has the value true).”说明只有在两个引用指向同一个对象时才会返回true。晕了晕了
意思就是说,对于任何非空的引用x和y,equals()方法当且仅当x和y引用同一个对象的时候,所以A是对的.C的叙述与上面的定义不符,应该是错的。因为这个方法并不判断内容,只是判断这两个引用是否指向相同的对象
http://community.csdn.net/Expert/topic/4274/4274819.xml?temp=.2078363
http://community.csdn.net/Expert/topic/4295/4295511.xml?temp=.2989008
return (this == obj);
}