迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

在 Java 中比较两个整数

作者:迹忆客 最近更新:2023/11/13 浏览次数:

本文介绍了如何比较 Java 中的两个整数。

要比较 Java 中的整数值,我们可以使用 equals() 方法或 ==(等于运算符)。两者都用于比较两个值,但是 == 运算符检查两个整数对象的引用相等性,而 equal() 方法仅检查整数值(原始和非原始)。

因此,在比较整数值时,由开发人员在比较方法之间进行选择。让我们看一些例子。


在 Java 中使用 == 运算符比较两个整数值

在此示例中,我们采用两个原始整数,然后使用 == 运算符比较两个值。我们使用 Java 15 测试该示例。请参见下面的示例。

public class SimpleTesting {
  public static void main(String[] args) {
    int a = 18;
    int b = 18;
    if (a == b) {
      System.out.println("Both are equal");
    } else
      System.out.println("Not equal");
  }
}

输出:

Both are equal

使用 Java 中的 equals() 方法比较两个整数引用

我们可以使用 equals() 方法在 Java 中比较两个整数。如果两个对象相等,则返回 true;否则,它返回 false。请参见下面的示例。

public class SimpleTesting {
  public static void main(String[] args) {
    Integer a = new Integer(18);
    Integer b = new Integer(18);
    if (a.equals(b)) {
      System.out.println("Both are equal");
    } else
      System.out.println("Not equal");
  }
}

输出:

Both are equal

在 Java 中使用 equals() 方法比较两个整数

在这里,我们正在使用 equals()方法比较两个整数引用。

public class SimpleTesting {
  public static void main(String[] args) {
    Integer a = 10;
    Integer b = 10;
    if (a.equals(b)) {
      System.out.println("Both are equal");
    } else
      System.out.println("Not equal");
  }
}

输出:

Both are equal

在 Java 中不要使用 == 运算符比较两个整数引用

我们不应该使用 == 运算符来比较两个整数值,因为它会检查引用(对象)的相等性。

Java 缓存了 -128 到 127 范围内的 Integer 值。因此,当两个整数对象在此范围内具有相同的值时,== 比较器将返回 true,因为它们引用的是同一个对象。但对于超出此范围的任何值,它将返回 false

public class SimpleTesting {
  public static void main(String[] args) {
    Integer a = 18;
    Integer b = 18;
    if (a == b) {
      System.out.println("Both are equal");
    } else
      System.out.println("Not equal");
  }
}

输出:

Both are equal
public class SimpleTesting {
  public static void main(String[] args) {
    Integer a = 150;
    Integer b = 150;
    if (a == b) {
      System.out.println("Both are equal");
    } else
      System.out.println("Not equal");
  }
}

输出:

Not equal

正如你在上面看到的,我们不应该使用 == 来比较两个 Integer 值。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

Pandas 追加数据到 CSV 中

发布时间:2024/04/24 浏览次数:352 分类:Python

本教程演示了如何在追加模式下使用 to_csv()向现有的 CSV 文件添加数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便