How compare enum java

How compare enum java

This is the common base class of all Java language enumeration types. More information about enums, including descriptions of the implicitly declared methods synthesized by the compiler, can be found in section 8.9 of The Java™ Language Specification . Note that when using an enumeration type as the type of a set or as the type of the keys in a map, specialized and efficient set and map implementations are available.

Constructor Summary

Method Summary

Returns the ordinal of this enumeration constant (its position in its enum declaration, where the initial constant is assigned an ordinal of zero).

Methods declared in class java.lang.Object

Constructor Detail

Enum

Sole constructor. Programmers cannot invoke this constructor. It is for use by code emitted by the compiler in response to enum type declarations.

Method Detail

name

Returns the name of this enum constant, exactly as declared in its enum declaration. Most programmers should use the toString() method in preference to this one, as the toString method may return a more user-friendly name. This method is designed primarily for use in specialized situations where correctness depends on getting the exact name, which will not vary from release to release.

ordinal

Returns the ordinal of this enumeration constant (its position in its enum declaration, where the initial constant is assigned an ordinal of zero). Most programmers will have no use for this method. It is designed for use by sophisticated enum-based data structures, such as EnumSet and EnumMap .

toString

Returns the name of this enum constant, as contained in the declaration. This method may be overridden, though it typically isn’t necessary or desirable. An enum type should override this method when a more «programmer-friendly» string form exists.

equals

hashCode

public final int hashCode()

clone

protected final Object clone() throws CloneNotSupportedException

Throws CloneNotSupportedException. This guarantees that enums are never cloned, which is necessary to preserve their «singleton» status.

compareTo

Compares this enum with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. Enum constants are only comparable to other enum constants of the same enum type. The natural order implemented by this method is the order in which the constants are declared.

getDeclaringClass

Returns the Class object corresponding to this enum constant’s enum type. Two enum constants e1 and e2 are of the same enum type if and only if e1.getDeclaringClass() == e2.getDeclaringClass(). (The value returned by this method may differ from the one returned by the Object.getClass() method for enum constants with constant-specific class bodies.)

valueOf

public static Enum> T valueOf​(Class enumType, String name)

Returns the enum constant of the specified enum type with the specified name. The name must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.) Note that for a particular enum type T , the implicitly declared public static T valueOf(String) method on that enum may be used instead of this method to map from a name to the corresponding enum constant. All the constants of an enum type can be obtained by calling the implicit public static T[] values() method of that type.

Читайте также:  План изучения языка программирования python

finalize

protected final void finalize()

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

Источник

How to Compare Two Enum in Java? Equals vs == vs CompareTo Example

How do I compare two enum in Java? Should I use == operator or equals() method? What is difference between comparing enum with == and equals() method are some of the tricky Java questions. Until you have solid knowledge of Enum in Java, It can be difficult to answer these question with confidence. By the way unlik e comparing String in Java, you can use both == and equals() method to compare Enum, they will produce same result because equals() method of Java.lang.Enum internally uses == to compare enum in Java. Since ev ery Enum in Java im plicitly extends java.lang.Enum ,and since equals() method is de clared final, there is no chance of overriding equals method in user defined enum. If you are not just checking whether two enums are equal or not, and rather interested in the order of different instances of Enum, then you can us e compareTo() method of e num to compare two enums.

Java.lang.Enum implements Comparable interface and imple ments compareTo() method. Natural order of enum is defined by the order they are declared in Java code and same order is returned by ordinal() method.

Comparing Enum with equals and == operator? Example

Compare Enum in Java - sorting - difference between equals and compareTo

As I said earlier, equals method of java.lang.Enum (see below code) uses == operator to check if two enum are equal. This means, You can compare Enum using both == and equals method. Also equals() method is declared final inside java.lang.Enum , so risk of overriding equals method.

public final boolean equals(Object other) < return this==other; >

By the way there are subtle difference when you compare enum with == and equals method, which stems from == (equality operator) being operator and equals() being method. Some of these points are already discussed in difference between equals and == in Java, but we will see them here with respect to comparing Enums in Java.

This one is easy to guess, but same time provide worth of money. If you compare any Enum with null, using == operator, it will result in false, but if you use equals() method to do this check, you may get NullPointerException , unless you are using calling equals in right way as shown in how to avoid NullPointerException in Java.

Читайте также:  PDF in HTML

Look at below code, here we are comparing an unknown Shape object with Shape enum which contains CIRCLE , RECTANGLE etc.

private enum Shape< RECTANGLE, SQUARE, CIRCLE, TRIANGLE; > private enum Status< ON, OFF; > Shape unknown = null; Shape circle = Shape.CIRCLE; boolean result = unknown == circle; //return false result = unknown.equals(circle); //throws NullPointerException

I agree this can be avoided by simply comparing known to unknown i.e. circle.equals(unknown), but this is one of the most common coding error Java programmers make . By using == to compare enum, you can completely avoid it.

Another advantage of using == to compare enum is, compile time safety. Equality or == operator checks if both enum object are from same enum type or not at compile time itself, while equals() method will also return false but at runtime. Since it’s always better to detect errors at compile time, == scores over equals in case of comparing enum.

If you are usin g Eclipse or Netbeans , you can detect these error as soon as you type. By the way Netbeans also shows warning when you call equals() method on two incomparable types, but that is completely IDE specific.

This is more from common sense, using operator should be a touch faster than calling method, and than using operator. Though I believe modern JIT compiler might inline equals() method, when you compare two enums in Java. Which means this would not be big difference in terms of performance.But, without any smartness from compiler or JVM, I think == should always be touch faster.

Comparing Enums with compareTo method

When we say comparing enum, it’s not always checking if two enums are equal or not. Sometime you need to compare them for sorting or to arrange them in a particular order. We know that we can compare objects usi ng Comparable and Comparator in Java and enum is no different, though it provides additional convenience. Java.lang.Enum implements Comparable interface and it’s compareTo() method compares only same type of enum. Also natural order of enum is the order in which they are declared in code.

As shown o n 10 examples of Enum in Java, s ame order is also maintained by ordinal() method of enum, which is used by EnumSet and EnumMap .

public final int compareTo(E o) < Enum other = (Enum)o; Enum self = this; if (self.getClass() != other.getClass() && // optimization self.getDeclaringClass() != other.getDeclaringClass()) throw new ClassCastException(); return self.ordinal - other.ordinal; >

That’s all on How to compare two enum in Java and difference between == and equals to compare two enums. Though using equals() to compare object is considere d Java best practice, c omparing Enum using == is better than using equals. Don’t forget ordinal() and compareTo() methods, which is also key to get natural order of Enum during comparison.

If you want to learn more about Enum, I suggest reading following Java books. Books are best resource to learn deep about any topic and I personally follow them as well. Enumeration types chapter from Thinking in Java is particularly useful.

Читайте также:  Php mysql local socket

3 comments :

Gr8 article, it will help a developers to understand the concepts more. few things that I want to like add is.

Yes: enums have tight instance controls that allows you to use == to compare instances. Here’s the guarantee provided by the language specification:

An enum type has no instances other than those defined by its enum constants.

It is a compile-time error to attempt to explicitly instantiate an enum type. The final clone method in Enum ensures that enum constants can never be cloned, and the special treatment by the serialization mechanism ensures that duplicate instances are never created as a result of deserialization. Reflective instantiation of enum types is prohibited. Together, these four things ensure that no instances of an enum type exist beyond those defined by the enum constants.

Because there is only one instance of each enum constant, it is permissible to use the == operator in place of the equals method when comparing two object references if it is known that at least one of them refers to an enum constant. (The equals method in Enum is a final method that merely invokes super.equals on its argument and returns the result, thus performing an identity comparison.)

This guarantee is strong enough that Josh Bloch recommends, that if you insist on using the singleton pattern, the best way to implement it is to use a single-element enum (see: Effective Java 2nd Edition, Item 3: Enforce the singleton property with a private constructor or an enum type; also Thread safety in Singleton)

What are the differences between == and equals?

As a reminder, it needs to be said that generally, == is NOT a viable alternative to equals. When it is, however (such as with enum), there are two important differences to consider:

== never throws NullPointerException

Color nothing = null;
if (nothing == Color.BLACK); // runs fine
if (nothing.equals(Color.BLACK)); // throws NullPointerException
== is subject to type compatibility check at compile time

if (Color.BLACK.equals(Chiral.LEFT)); // compiles fine
if (Color.BLACK == Chiral.LEFT); // DOESN’T COMPILE. Incompatible types!
Should == be used when applicable?

Bloch specifically mentions that immutable classes that have proper control over their instances can guarantee to their clients that == is usable. enum is specifically mentioned to exemplify.

Item 1: Consider static factory methods instead of constructors

[. ] it allows an immutable class to make the guarantee that no two equal instances exist: a.equals(b) if and only if a==b. If a class makes this guarantee, then its clients can use the == operator instead of the equals(Object) method, which may result in improved performance. Enum types provide this guarantee.

To summarize, the arguments for using == on enum are:

It works.
It’s faster.
It’s safer at run-time.
It’s safer at compile-time.

As always Great Comment Saral, and thanks for adding value and sharing that JLS reference.

Hello there, == breaks type abstraction where equals() does not.
In other words your implementation is now married to your interface and all of those ==’s will break the moment you need to define a super interface.

Источник

Оцените статью