> ## Content Index
> Fetch the complete content index at: https://www.narendravardi.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Java Shot #2: Prefer using equality operator over equals() method for enums
- URL: https://www.narendravardi.com/prefer-using-equality-operator-over-equals-method-for-enums/
- Published: 2024-06-28T10:30:01.000Z
- Updated: 2024-12-02T08:44:38.000Z
- Author: Narendra Vardi
- Tags: java-shot, 2024

Enums are a powerful datatype in Java. There are many instances where you want to compare two enums. Given that a enum class supports `equals()` method, we end up using `equals()` method as it's frequently used for other object comparisons. 

![](https://storage.ghost.io/c/d9/fa/d9faef57-e1a8-4faa-a907-40713e9d52a6/content/images/2024/06/Screenshot-2024-06-07-at-4.27.13-PM.png)

Even IntelliJ's AI recommendation is to use equals() method 🤭

In case of enums, using `==` (equality) operator is a best practice. 

Why? Let's look at the below code.

```Java

class Main {
  enum NumberType {
    PRIME,
    COMPOSITE
  }

  // returns NumberType.PRIME if number is prime
  // returns NumberType.COMPOSITE if the number is composite
  // else returns null
  //
  // This method is buggy since this doesn't have any validations on the numbers.
  // You can consider this method you are using from an external library.
  static NumberType getNumberType(Integer number) {
    // function logic for prime and composite numbers.
    return null;  
  }

  public static void main(String[] args) {
    for (int i = 0; i < 10; i++) {
      // 0 and 1 and negative numbers cannot be categorised into prime or composite and hence it
      // leads to
      // NullPointerException when using equals() method.
      if (getNumberType(i).equals(NumberType.PRIME)) {
        System.out.printf("%d is composite", i);
      } else if (getNumberType(i).equals(NumberType.COMPOSITE)) {
        System.out.printf("%d is composite", i);
      } else {
        System.out.printf("%d is neither prime nor composite", i);
      }
    }
  }
}

```

If you execute this program, this will throw NullPointerException (NPE). 

This can be easily avoided by making use of `==` operator. 

```Java

class Main {
  enum NumberType {
    PRIME,
    COMPOSITE
  }

  // returns NumberType.PRIME if number is prime
  // returns NumberType.COMPOSITE if the number is composite
  // else returns null
  //
  // This method is buggy since this doesn't have any validations on the numbers.
  // You can consider this method you are using from an external library.
  static NumberType getNumberType(Integer number) {
    // function logic for prime and composite numbers.
    return null;
  }

  public static void main(String[] args) {
    for (int i = 0; i < 10; i++) {
      // 0 and 1 and negative numbers cannot be categorised into prime or composite and hence it
      // leads to
      // NullPointerException when using equals() method.
      if (getNumberType(i) == NumberType.PRIME) {
        System.out.printf("%d is composite\n", i);
      } else if (getNumberType(i) == NumberType.COMPOSITE) {
        System.out.printf("%d is composite\n", i);
      } else {
        System.out.printf("%d is neither prime nor composite\n", i);
      }
    }
  }
}

```

Why this works? For the simple reason that Enum values are singletons and they have a single reference, `==` works for them. 

There is also a different way you can avoid NPE when using `equals()` method. 

Can you guess the approach?