---
title: "Comparing Java enum members: == or equals()?"  
description: "Comparing Java enum members: == or equals()?"  
author: "Samuel Fernandes"  
published: 2015-05-16  
updated: 2015-05-16  
canonical: https://www.mindstick.com/forum/23233/comparing-java-enum-members-or-equals  
category: "java"  
tags: ["java"]  
reading_time: 1 minute  

---

# Comparing Java enum members: == or equals()?

I know that Java enums are compiled to classes with [private constructors](https://www.mindstick.com/interview/2338/private-constructors-can-be-used-in-the-singleton-design-pattern) and a bunch of public [static](https://www.mindstick.com/articles/12098/the-static-keyword-the-static-methods) members. When comparing two members of a given [enum](https://www.mindstick.com/forum/159626/how-can-i-cast-a-string-to-an-enum), I've always used .[equals](https://www.mindstick.com/interview/33895/difference-between-and-equals-method-in-c-sharp)(), e.g.

```
public useEnums(SomeEnum a){    if(a.equals(SomeEnum.SOME_ENUM_VALUE))    {        ...    }    ...}
```

However, I just came across come [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) that uses the equals [operator](https://www.mindstick.com/blog/144/union-intersection-and-except-operator-in-sql-server) == instead:\

```
public useEnums2(SomeEnum a){    if(a == SomeEnum.SOME_ENUM_VALUE)    {        ...    }    ...}
```

I've been [programming](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming) in Java for 5+ years, and I thought I understood [difference](https://www.mindstick.com/articles/157114/good-news-or-bad-news-and-the-difference-is) between the two - but I'm still scratching my head at which one is more correct. Which operator is the one I should be using?

## Replies

### Reply by Anonymous User

Both are technically correct. If you look at the source code for .equals(), it simply defers to ==.

I use ==, however, as that will be null safe.


---

Original Source: https://www.mindstick.com/forum/23233/comparing-java-enum-members-or-equals

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
