如何在Java中正确比较整数

发布于 2024-12-21 10:30:39 字数 1555 浏览 2 评论 0原文

可能的重复:
整数包装类和 == 运算符 - 在哪里指定行为?< /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。我很困惑。

此外,ListcustomerNotice 通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅沫记忆 2024-12-28 10:30:39

简短:(回答问题)

如果您想将Integers作为对象进行比较,您应该使用.equals

i.equals(j);
m.equals(n);

这样,它们都应该返回。但如果你真的想使用==,你需要获取原始的int值:

i.intValue() == j.intValue();
m.intValue() == j.intValue();

LONG:(解释答案)

这个的基础是 Object 总是单独存储在内存中(除了一些特殊情况,如 m=n),并且为了正确比较,它们需要被分解为原始类型可以使用 == 成功比较的类型。

每个Object都有一个.equals()方法,该方法继承自Object作为其超类。但是,必须重写它才能进行正确的比较。 Integer 重写此方法以成功与 Integer 对象进行比较,同时使用 == 检查两个对象是否指向内存中的同一空间,并且由于 Object 的两个实例不能指向内存中的同一空间,因此这将始终返回 false

但是,正如您的代码所指出的,有一些特殊情况是有效的,例如:

  1. 您的代码使用 Integer i = 1,它被视为“标准实例”,并且可以使用<代码>==。
  2. 如果您使用 = 将一个 Object 设置为与另一个对象相等,Java 会告诉这两个对象指向内存中的同一位置,这意味着 ==将返回true

还有很多其他的,但我想到并且似乎相关的就是这两个。

SHORT: (answers question)

If you want to compare Integers as the objects, you should use .equals:

i.equals(j);
m.equals(n);

With this, they should both return true. But if you really want to use ==, you need to get the primitive int value:

i.intValue() == j.intValue();
m.intValue() == j.intValue();

LONG: (explains answer)

The basis of this is that Objects are always stored separately in memory (except for some special cases like m=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 from Object as its superclass. However, it must be overridden to do a proper comparison. Integer overrides this method to compare to Integer objects successfully, while using == checks to see if both objects point to the same space in memory, and because two instances of an Object cannot point to the same space in memory, this will always return false.

However, as your code points out, there are some special cases that work, like these:

  1. Your code uses a Integer i = 1, which is considered a "standard instance" and is able to be compared using ==.
  2. If you set one Object equal to another using =, Java tells both objects to point to the same location in memory, which means that == will return true.

There are many others, but those are the two that come to mind and seem relevant.

始终不够爱げ你 2024-12-28 10:30:39

你会让自己发疯,并浪费大量时间试图找出这种方法有效或无效的具体情况。这取决于您并不总是可见的代码的实现。

底线:永远,永远,使用 == 来比较 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 compare Integer 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 an int, and then you can use == to compare that int to another int.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文