在 Java 中使用 == 或 equals() 方法比较 Java 枚举
本教程介绍如何使用 Java 中的 ==
运算符或 equals()
方法比较 Java 枚举。
枚举是一组常量,用于收集日、月、颜色等数据集。在 Java 中,要创建枚举,我们使用 enum 关键字,然后为类型提供值。
本文将演示如何比较枚举值和对象。让我们通过一些例子来理解。
在 Java 中使用 ==
运算符比较枚举
==
(等于)运算符是需要两个操作数的二元运算符。它比较操作数并返回 true
或 false
。
我们可以使用它来比较枚举值。请参见下面的示例。
enum Color {
red,
green,
yello,
white,
black,
purple,
blue;
}
public class SimpleTesting {
public static void main(String[] args) {
boolean result = isGreen(Color.blue);
System.out.println(result);
result = isGreen(Color.green);
System.out.println(result);
}
public static boolean isGreen(Color color) {
if (color == Color.green) {
return true;
}
return false;
}
}
输出:
false
true
在 Java 中使用 equals()
方法比较枚举值
Java equals()
方法比较两个值并返回一个布尔值,true
或 false
。我们可以使用这种方法来比较枚举值。
在这里,我们使用 Color
枚举来比较它的值。第一个值返回 false
,但第二个返回 true
。请参见下面的示例。
enum Color {
red,
green,
yello,
white,
black,
purple,
blue;
}
public class SimpleTesting {
public static void main(String[] args) {
boolean result = isGreen(Color.blue);
System.out.println(result);
result = isGreen(Color.green);
System.out.println(result);
}
public static boolean isGreen(Color color) {
if (color.equals(Color.green)) {
return true;
}
return false;
}
}
输出:
false
true
比较枚举值并处理 null
安全性
在 Java 中,最成问题的问题是处理 null
值。它也适用于枚举比较;如果我们使用 equals()
方法比较枚举值并且枚举对象是 null
,它会抛出 nullpointerexception
。请参见下面的示例。
enum Color {
red,
green,
yello,
white,
black,
purple,
blue;
}
public class SimpleTesting {
public static void main(String[] args) {
Color color = null;
boolean result = isGreen(color);
System.out.println(result);
result = isGreen(Color.green);
System.out.println(result);
}
public static boolean isGreen(Color color) {
if (color.equals(Color.green)) {
return true;
}
return false;
}
}
输出:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "javaexample.Color.equals(Object)" because "color" is null
但是,如果我们使用 ==
(等于)运算符并比较枚举值/对象,它不会抛出 nullpointerexception
。这意味着该运算符是 null
安全的,并且比 equals()
方法更易于使用。请参见下面的示例。
enum Color {
red,
green,
yello,
white,
black,
purple,
blue;
}
public class SimpleTesting {
public static void main(String[] args) {
Color color = null;
boolean result = isGreen(color);
System.out.println(result);
result = isGreen(Color.green);
System.out.println(result);
}
public static boolean isGreen(Color color) {
if (color == Color.green) {
return true;
}
return false;
}
}
输出:
false
true
在 Java 中比较两个不同的枚举值
我们还可以使用 equals()
方法比较两个枚举对象。由于两个对象不同,因此在两种情况下都返回 false
。请参见下面的示例。
enum Color {
red,
green,
yello,
white,
black,
purple,
blue;
}
enum MyColors {
green,
red,
blue;
}
public class SimpleTesting {
public static void main(String[] args) {
boolean result = isGreen(Color.red);
System.out.println(result);
result = isGreen(Color.green);
System.out.println(result);
}
public static boolean isGreen(Color color) {
if (color.equals(MyColors.red)) {
return true;
}
return false;
}
}
输出:
false
false
相关文章
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() 函数的使用。