System.out.println(new Integer(1).equals(new Long(1)));为什么是这个答案?????

wenjjing2lianee 2008-12-07 11:34:24
System.out.println(new Integer(1).equals(new Long(1)));
为什么是这个答案?????
...全文
299 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
andyheway 2008-12-08
  • 打赏
  • 举报
回复
public boolean equals(Object obj)
{
if(obj instanceof Integer)
return value == ((Integer)obj).intValue();
else
return false;
}
正解。直接就是false。
wenjjing2lianee 2008-12-08
  • 打赏
  • 举报
回复
谢谢各位!!!!!
ahlon 2008-12-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zhj92lxs 的回复:]
/**
* Compares this object to the specified object. The result is
* <code>true </code> if and only if the argument is not
* <code>null </code> and is an <code>Integer </code> object that
* contains the same <code>int </code> value as this object.
*
* @param obj the object to compare with.
* @return <code>true </code> if the objects are the same;

[/Quote]
对,是由于Integer类的equals方法决定的
zhj92lxs 2008-12-07
  • 打赏
  • 举报
回复
/**
* Compares this object to the specified object. The result is
* <code>true</code> if and only if the argument is not
* <code>null</code> and is an <code>Integer</code> object that
* contains the same <code>int</code> value as this object.
*
* @param obj the object to compare with.
* @return <code>true</code> if the objects are the same;
* <code>false</code> otherwise.
*/
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}



这是Integer对equals 方法的重写,看看就知道了
ty_fzpb 2008-12-07
  • 打赏
  • 举报
回复
你读一下equals源代码就知道了……

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧