比较Java中的两个对象
我有两个相同实体“社区”的不同对象 两个对象(community和com)具有相同的值 community.java具有以下变量:比较Java中的两个对象,java,Java,我有两个相同实体“社区”的不同对象 两个对象(community和com)具有相同的值 community.java具有以下变量: private Integer communityId; private String communityName; private String description; // many to many relationship private Set<Faculty> faculties = new HashSet&l
private Integer communityId;
private String communityName;
private String description;
// many to many relationship
private Set<Faculty> faculties = new HashSet<Faculty>();
private Set<User> users = new HashSet<User>();
当我选中
community==com
时,它返回false。。为什么?我犯了什么错误?这两个对象都是从数据库中检索到的 因为它们指的不是同一个对象<代码>=用于检查两者是否引用同一对象
==
对于对象,请参考相同的
等于内容等效性
试试这个
return community.getCommunityId().equals(this.getCommunityId());
因为,它们指的不是同一个对象<代码>=
用于检查两者是否引用同一对象
==
对于对象,请参考相同的
等于内容等效性
试试这个
return community.getCommunityId().equals(this.getCommunityId());
=
比较指向对象的链接
您应该显式调用community.equals(com)
。还要注意空检查。==
比较指向对象的链接
您应该显式调用community.equals(com)
。还要注意空检查
当我检查community==com时,它返回false。。为什么
这意味着;这两个参考文献完全相同吗。i、 同一个物体。你的意图是
boolean equal = community.equals(com);
顺便说一句,如果(obj==null)
检查是多余的
当我检查community==com时,它返回false。。为什么
这意味着;这两个参考文献完全相同吗。i、 同一个物体。你的意图是
boolean equal = community.equals(com);
顺便说一句,如果(obj==null)检查是多余的。因为您使用
=
而不是equals()比较对象(ID)<代码>=
测试两个变量是否引用同一对象equals()
测试两个变量是否引用两个功能上相等的整数(即具有相同的int值)
除了枚举之外,使用
==
比较对象几乎总是一个错误。因为您使用=
而不是equals()比较对象(ID)<代码>=
测试两个变量是否引用同一对象equals()
测试两个变量是否引用两个功能上相等的整数(即具有相同的int值)
除了枚举之外,使用
==
比较对象几乎总是一个错误。equals方法的问题是,对对象使用=
运算符。这里CommunityId必须是同一个对象才能返回true:
return community.getCommunityId() == this.getCommunityId();
应该是
return community.getCommunityId().equals(this.getCommunityId());
equals方法的问题是,您正在对对象使用
=
运算符。这里CommunityId必须是同一个对象才能返回true:
return community.getCommunityId() == this.getCommunityId();
应该是
return community.getCommunityId().equals(this.getCommunityId());
=
比较可能指向两个不同位置的两个引用,而与对象内容无关
您应该使用community.equals(com)
检查是否相等'
此外,equals方法包含以下段:
community.getCommunityId() == this.getCommunityId()
由于communityId
是一个Integer
对象,因此=
运算符可能会对因插入而不在[-127,128]范围内的整数值给出负结果,这是一个单独的概念,您可以稍后进行检查
您还需要在那里使用equals()
,或者比较。intValue()
=
比较可能指向两个不同位置的两个引用,而与对象内容无关
您应该使用community.equals(com)
检查是否相等'
此外,equals方法包含以下段:
community.getCommunityId() == this.getCommunityId()
由于communityId
是一个Integer
对象,因此=
运算符可能会对因插入而不在[-127,128]范围内的整数值给出负结果,这是一个单独的概念,您可以稍后进行检查
您还需要在那里使用equals()
,或者比较。intValue()
==Java中的运算符比较两个对象的内存地址,在您的情况下,comm和community必须是存储在两个不同内存地址的两个不同对象==Java中的运算符比较两个对象的内存地址,在您的情况下,comm和community必须是存储在两个不同内存地址的两个不同对象您正在比较两个不同对象communityId。 是否需要将communityId声明为整数?因为Integer是一个对象。 为什么不简单地用一个基本类型int声明communityId;
int communityId应该可以工作。您正在比较communityId的两个不同对象。 是否需要将communityId声明为整数?因为Integer是一个对象。 为什么不简单地用一个基本类型int声明communityId;
int communityId应该可以工作。您是否重写了public int hashCode()号的可能重复项。。我没有重写hashCode(),但是在上面定义的equals()的返回语句中使用equals()是有效的!u是否重写了public int hashCode()号的可能重复项。。我没有重写hashCode(),但是在上面定义的equals()的返回语句中使用equals()是有效的!在使用community.equals(com)和community.getCommunityId()以及com.getCommunityId之后,仍然会显示false!您应该检查我的答案以了解可能的原因,尝试将
equals
方法中的最后一条语句更改为返回此.getCommunityId().equals(community.getCommunityId())
如另一个答案中所述,Integer
s与int
s不同,它还需要equals
方法才能正确比较。使用community.equals(com)和community.getCommunityId()以及com.getCommunityId之后,它仍然表示false!您应该检查我的答案以了解可能的原因,尝试将equals
方法中的最后一条语句更改为返回此.getCommunityId().equals(community.getCommunityId())有限公司