K
Knute Johnson
What is the correct method to test equality with an enum, == or .equals?
Thanks,
Thanks,
Knute said:What is the correct method to test equality with an enum, == or .equals?
Chris said:Knute said: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 said:Knute said: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 ==.
Knute said:What is the correct method to test equality with an enum, == or .equals?
Esmond Pitt said:== 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 said: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.
Esmond Pitt said: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
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.