enum equality?

Discussion in 'Java' started by Knute Johnson, Jan 25, 2007.

  1. What is the correct method to test equality with an enum, == or .equals?

    Thanks,

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Jan 25, 2007
    #1
    1. Advertising

  2. Knute Johnson

    Chris Uppal Guest

    Knute Johnson wrote:

    > What is the correct method to test equality with an enum, == or .equals?


    For most purposes == is correct.

    It's the nature of "enum"[*] objects that each object is distinct and, in some
    sense, unique (sort of like the Singleton pattern). So you will normally want
    to compare the identity of two object references -- hence ==.

    -- chris

    [*] rather a misleading name since it suggests a non-existent similarity to C
    or C++'s enums.
     
    Chris Uppal, Jan 25, 2007
    #2
    1. Advertising

  3. Chris Uppal wrote:
    > Knute Johnson wrote:
    >
    >> What is the correct method to test equality with an enum, == or .equals?

    >
    > For most purposes == is correct.
    >
    > It's the nature of "enum"[*] objects that each object is distinct and, in some
    > sense, unique (sort of like the Singleton pattern). So you will normally want
    > to compare the identity of two object references -- hence ==.
    >
    > -- chris
    >
    > [*] rather a misleading name since it suggests a non-existent similarity to C
    > or C++'s enums.
    >
    >


    Thanks Chris.

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Jan 25, 2007
    #3
  4. Knute Johnson

    Oliver Wong Guest

    "Chris Uppal" <-THIS.org> wrote in message
    news:45b8cc14$0$756$...
    > Knute Johnson wrote:
    >
    >> What is the correct method to test equality with an enum, == or .equals?

    >
    > For most purposes == is correct.
    >
    > It's the nature of "enum"[*] objects that each object is distinct and, in
    > some
    > sense, unique (sort of like the Singleton pattern). So you will normally
    > want
    > to compare the identity of two object references -- hence ==.


    The two are *almost* equivalent. The java.lang.Enum class (which all
    enums extend) makes the equals() method final, and the implementation is
    "return this == other;".

    The only situation I can think of where they differ is that an enum
    reference may be null, and so the equals() method invocation may result in a
    NullPointerException.

    So I agree with Chris: == is probably what most people want most of the
    time.

    - Oliver
     
    Oliver Wong, Jan 25, 2007
    #4
  5. Knute Johnson

    Esmond Pitt Guest

    Knute Johnson wrote:
    > What is the correct method to test equality with an enum, == or .equals?


    == will do. There is a bug if you have deserialized the enum, otherwise
    Java takes care that there is only one instance of each value.
     
    Esmond Pitt, Jan 29, 2007
    #5
  6. Esmond Pitt <> wrote:
    > Knute Johnson wrote:
    >> What is the correct method to test equality with an enum, == or .equals?

    >
    >== will do. There is a bug if you have deserialized the enum, otherwise
    > Java takes care that there is only one instance of each value.


    Please more info about that bug!

    It would look like a very severe bug, if the property of enums
    (that there can't ever exist any other instances than those
    created at enum class initialization time) was so easily broken.
     
    Andreas Leitgeb, Jan 29, 2007
    #6
  7. Knute Johnson

    Daniel Dyer Guest

    On Mon, 29 Jan 2007 15:45:51 -0000, Andreas Leitgeb
    <> wrote:

    > Esmond Pitt <> wrote:
    >> Knute Johnson wrote:
    >>> What is the correct method to test equality with an enum, == or
    >>> .equals?

    >>
    >> == will do. There is a bug if you have deserialized the enum, otherwise
    >> Java takes care that there is only one instance of each value.

    >
    > Please more info about that bug!
    >
    > It would look like a very severe bug, if the property of enums
    > (that there can't ever exist any other instances than those
    > created at enum class initialization time) was so easily broken.


    There is this bug concerning IIOP, there may also be issues when IIOP is
    not involved:

    http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6277781

    I worked around the problem by adding the following method to all enums
    that are likely to be serialized:

    private Object readResolve()
    {
    return valueOf(name());
    }

    It substitutes the correct enum constant in place of any deserialized enum
    object with the same name.

    Dan.

    --
    Daniel Dyer
    http://www.uncommons.org
     
    Daniel Dyer, Jan 29, 2007
    #7
  8. Knute Johnson

    Esmond Pitt Guest

    Andreas Leitgeb wrote:
    > Esmond Pitt <> wrote:
    >>
    >>== will do. There is a bug if you have deserialized the enum, otherwise
    >>Java takes care that there is only one instance of each value.

    >
    >
    > Please more info about that bug!
    >
    > It would look like a very severe bug, if the property of enums
    > (that there can't ever exist any other instances than those
    > created at enum class initialization time) was so easily broken.


    It is in RMI/IIOP actually, not just Serialization per se, and it
    requires an OMG CORBA Java/IDL specification update to fix it ;-)
    See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6277781
    and http://forum.java.sun.com/thread.jspa?forumID=62&threadID=744600
     
    Esmond Pitt, Jan 30, 2007
    #8
  9. Esmond Pitt <> wrote:
    > Andreas Leitgeb wrote:
    >> Please more info about that bug!

    > It is in RMI/IIOP actually, not just Serialization per se, and it
    > requires an OMG CORBA Java/IDL specification update to fix it ;-)
    > See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6277781
    > and http://forum.java.sun.com/thread.jspa?forumID=62&threadID=744600


    Thanks!

    Serializing enums seems to be a fragile thing for enums
    that have instance fields. Either the serialized objects
    data or the target-JVM-objects data would need to get lost.
    As it seems it's the original objects one for plain (non-
    corba) serialisation. And it's a much worse mess with Corba
    and the information bottleneck "sdl".
     
    Andreas Leitgeb, Jan 30, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -

    enum within an enum

    -, Jun 12, 2005, in forum: Java
    Replies:
    6
    Views:
    578
  2. Jerminia
    Replies:
    3
    Views:
    652
    Roedy Green
    Oct 7, 2005
  3. Ernst Murnleitner

    How to enum an enum?

    Ernst Murnleitner, Nov 12, 2003, in forum: C++
    Replies:
    5
    Views:
    493
    Rolf Magnus
    Nov 13, 2003
  4. mrhicks
    Replies:
    2
    Views:
    446
    Dave Thompson
    Jun 10, 2004
  5. Randy
    Replies:
    1
    Views:
    526
    David Harmon
    Jan 7, 2006
Loading...

Share This Page