迹忆客 专注技术分享

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

在 Java 中使用 == 或 equals() 方法比较 Java 枚举

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

本教程介绍如何使用 Java 中的 == 运算符或 equals() 方法比较 Java 枚举。

枚举是一组常量,用于收集日、月、颜色等数据集。在 Java 中,要创建枚举,我们使用 enum 关键字,然后为类型提供值。

本文将演示如何比较枚举值和对象。让我们通过一些例子来理解。


在 Java 中使用 == 运算符比较枚举

==(等于)运算符是需要两个操作数的二元运算符。它比较操作数并返回 truefalse

我们可以使用它来比较枚举值。请参见下面的示例。

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() 方法比较两个值并返回一个布尔值,truefalse。我们可以使用这种方法来比较枚举值。

在这里,我们使用 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

转载请发邮件至 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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便