如何在Java中正确比较整数
可能的重复:
整数包装类和 == 运算符 - 在哪里指定行为?< /a>
我知道Java整数使用缓存在-127~128。 如果
Integer i = 1;
Integer j = 1;
Integer m = 128;
Integer n = 128;
i == j // true
m == n // false
但是我遇到了一个奇怪的现象。首先,看下面的片段。
List<CustomerNotice> customerNotice = findByExample(example); // use Hibernate findByExample method
for(CustomerNotice n : customerNotice){
if(n.getConfirmStatus() == NoticeConfirmStatus.UNCONFIRMED.getValue()){
// do sth
}
}
public enum NoticeConfirmStatus{
UNCONFIRMED(1), //
CONFIRMED(2), //
FAILED_TO_CONFIRM(3); //
private final Integer value;
private NoticeConfirmStatus(Integer value) {
this.value = value;
}
public Integer getValue() {
return this.value;
}
}
public class CustomerNotice {
@Column(name = "CONFIRM_STATUS")
private Integer confirmStatus;
public Integer getConfirmStatus() {
return this.confirmStatus;
}
public void setConfirmStatus(Integer confirmStatus) {
this.confirmStatus = confirmStatus;
}
}
虽然不推荐使用 if 表达式,但我认为它会返回 true,因为 n.getConfirmStatus()==1
,但结果是 false。我很困惑。
此外,List
通过 Hibernate findByExample 方法获取。检索结果集时是否有一些自动装箱或新操作?
谢谢。
Possible Duplicate:
Integer wrapper class and == operator - where is behavior specified?
I known Java integer use cache in -127~128.
If
Integer i = 1;
Integer j = 1;
Integer m = 128;
Integer n = 128;
i == j // true
m == n // false
But I met a strange phenomenon.First,look at following snippet.
List<CustomerNotice> customerNotice = findByExample(example); // use Hibernate findByExample method
for(CustomerNotice n : customerNotice){
if(n.getConfirmStatus() == NoticeConfirmStatus.UNCONFIRMED.getValue()){
// do sth
}
}
public enum NoticeConfirmStatus{
UNCONFIRMED(1), //
CONFIRMED(2), //
FAILED_TO_CONFIRM(3); //
private final Integer value;
private NoticeConfirmStatus(Integer value) {
this.value = value;
}
public Integer getValue() {
return this.value;
}
}
public class CustomerNotice {
@Column(name = "CONFIRM_STATUS")
private Integer confirmStatus;
public Integer getConfirmStatus() {
return this.confirmStatus;
}
public void setConfirmStatus(Integer confirmStatus) {
this.confirmStatus = confirmStatus;
}
}
Although the if expression is not recommended, I think it will be return true,because n.getConfirmStatus()==1
, but the result is false.I'm very confusing.
In addition, theList<CustomerNotice> customerNotice
acquired by Hibernate findByExample method. Is there some Autoboxing or new operation when retrieve the resultset?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简短:(回答问题)
如果您想将
Integers
作为对象进行比较,您应该使用.equals
:这样,它们都应该返回
真
。但如果你真的想使用==
,你需要获取原始的int
值:LONG:(解释答案)
这个的基础是
Object
总是单独存储在内存中(除了一些特殊情况,如m=n
),并且为了正确比较,它们需要被分解为原始类型可以使用==
成功比较的类型。每个
Object
都有一个.equals()
方法,该方法继承自Object
作为其超类。但是,必须重写它才能进行正确的比较。Integer
重写此方法以成功与Integer
对象进行比较,同时使用==
检查两个对象是否指向内存中的同一空间,并且由于Object
的两个实例不能指向内存中的同一空间,因此这将始终返回false
。但是,正如您的代码所指出的,有一些特殊情况是有效的,例如:
Integer i = 1
,它被视为“标准实例”,并且可以使用<代码>==。=
将一个Object
设置为与另一个对象相等,Java 会告诉这两个对象指向内存中的同一位置,这意味着==
将返回true
。还有很多其他的,但我想到并且似乎相关的就是这两个。
SHORT: (answers question)
If you want to compare
Integers
as the objects, you should use.equals
:With this, they should both return
true
. But if you really want to use==
, you need to get the primitiveint
value:LONG: (explains answer)
The basis of this is that
Object
s are always stored separately in memory (except for some special cases likem=n
), and to be compared properly, they need to be broken down into primitive types that can be compared successfully using==
.Every
Object
has a.equals()
method, which is inherited fromObject
as its superclass. However, it must be overridden to do a proper comparison.Integer
overrides this method to compare toInteger
objects successfully, while using==
checks to see if both objects point to the same space in memory, and because two instances of anObject
cannot point to the same space in memory, this will always returnfalse
.However, as your code points out, there are some special cases that work, like these:
Integer i = 1
, which is considered a "standard instance" and is able to be compared using==
.Object
equal to another using=
, Java tells both objects to point to the same location in memory, which means that==
will returntrue
.There are many others, but those are the two that come to mind and seem relevant.
你会让自己发疯,并浪费大量时间试图找出这种方法有效或无效的具体情况。这取决于您并不总是可见的代码的实现。
底线:永远,永远,使用
==
来比较Integer
实例,句号。正如您所看到的,它有时在某些情况下有效,而在其余时间则严重失败。如果您有一个返回 Integer 的方法,则将该值分配给int
,然后您可以使用==
来比较该int
到另一个int
。You'll drive yourself crazy and waste a lot of time trying to figure out specific cases where this works or does not work. It depends on the implementation of code which isn't always visible to you.
The bottom line: never, ever, use
==
to compareInteger
instances, period. As you have seen, it works sometimes, under some circumstances, and fails miserably the rest of the time. If you have a method that returns an Integer, then assign the value to anint
, and then you can use==
to compare thatint
to anotherint
.