In Java, enum is a special Java type used to define collections of constants. More precisely, a Java enum type is a special kind of Java class. An enum can contain constants, methods etc. enum can be defined as a group of named constant.
There are two ways for making comparison of enum members :
- By using == operator
- By using equals() method
equals method uses == operator internally to check if two enum are equal. This means, You can compare Enum using both == and equals method. But the question is if we have two ways for comparing two members of enum, then which one is better?
Both == operator and .equals() method are meant for comparison only. But there are few differences :
- == operator never throws NullPointerException whereas .equals() method can throw NullPointerException.
- == is responsible for type compatibility check at compile time whereas .equals() method will never worry about the types of both the arguments.
Lets have a look on the programs for better understanding:
false Exception in thread "main" java.lang.NullPointerException
Explanation: In the above program, we are comparing an enum member with a null. When we use == operator for comparison, then we are not getting any Exception whereas when we use .equals() method then we are getting NullPointerException.
Therefore we can conclude that when we use == operator for enum member comparison, then it does not throw any Exception but .equals() method throw an Exception.
error: incomparable types: Month and Day
Explanation:In the above program, we are comparing an enum member with an another enum member. When we use .equals() method for comparison then we are not getting any Compilation Failure(CF) whereas when we use == operator then we are getting Compilation Failure(CF) saying “incomparable types” . Therefore we can conclude that when we use == operator for enum member comparison then it performs type compatibility check at compile time whereas .equals() method will never worry about the types of both the arguments(it will just give “false” for incomparable types).
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Java.util.BitSet class methods in Java with Examples | Set 2
- Shadowing of static functions in Java
- How does default virtual behavior differ in C++ and Java ?
- How are Java objects stored in memory?
- How are parameters passed in Java?
- Are static local variables allowed in Java?
- final variables in Java
- Default constructor in Java
- Assigning values to static final variables in Java
- Comparison of Exception Handling in C++ and Java
- Does Java support goto?
- Arrays in Java
- Inheritance and constructors in Java
- More restrictive access to a derived class method in Java
- Comparison of static keyword in C++ and Java
- Static blocks in Java
- Access specifiers for classes or interfaces in Java
- java.lang.Character class methods | Set 1
- Comparison of Inheritance in C++ and Java
- Output of Java Program | Set 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : rss009