在 Java 中比较两个整数
本文介绍了如何比较 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
值。
相关文章
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
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 中执行 SQL 查询
发布时间:2024/04/24 浏览次数:1195 分类:Python
-
本教程演示了在 Python 中对 Pandas DataFrame 执行 SQL 查询。
在 Pandas 中使用 stack() 和 unstack() 函数重塑 DataFrame
发布时间:2024/04/24 浏览次数:1289 分类:Python
-
本文讨论了 Pandas 中 stack() 和 unstack() 函数的使用。