[code="java"]public class Atest {
public static void main(String[] args)
{
String a = new String("test");
String b = new String("test");
System.out.println(a.equals(b));
System.out.println(a==b);
System.out.println(((Object)a).equals((Object)b));
System.out.println(Atest.compare(a, b));
System.out.println(Atest.isStr((Object)a));
}
public static boolean compare(Object a,Object b)
{
return a.equals(b);
}
public static boolean isStr(Object a)
{
if (a instanceof String)
{
return true;
}
return false;
}
}[/code]
代码运行结果为
true
false
true
true
true
我想知道为什么后三个是true.
String向上转化为Object后,调用的如果是Ojbect.equals的话,应该是false的。
Object的equals的实现
public boolean equals(Object obj) {
return (this == obj);
}