• Post Reply Bookmark Topic Watch Topic
  • New Topic

compare enums  RSS feed

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a enum:



What is the correct/fastest way to compare two enums:

this:



or



or



or




or



or




In all cases, the result is still the same (it returns true).

But what is the correct way to compare two enums, which way should I use?
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each item in an enum is like a singleton. There is exactly one instance of it.

So, comparing enums using the == operator is the way to go, and should also be the fastest.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you say:



is the correct and fastest way.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Judging from the fact that there is only one instance per item, using == is the logical consequence.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what about for example,different class loader or enums in web-applications?

In few tutorials they compare two enums with equals and toString-method.

Is "==" really the secure way or is equals more secure?
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check this site out --> http://leepoint.net/notes-java/data/expressions/22compareobjects.html

You'll find the word enum in the '==' section.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay thanky you!!

So I trust it and use "==" instead of equals.

thanks.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For enums yes. But don't use it on Strings like the return values of toString() or name().
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello rob,

thanks for the advice, now it is clear:

I sum up:

The right way to compare enums:

way 1:
if(t == Hobby.footbal)


way 2:


way 3:


all ways are correct, but way 1 is the fastest and shortest way.

 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes...
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is another option, using order():
I just thought I'd mention it; just as using name comparison it works but using simple == on the instance itself is best with enums.
 
Ken Severn
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sebastian Janisch wrote:check this site out --> http://leepoint.net/notes-java/data/expressions/22compareobjects.html

You'll find the word enum in the '==' section.


I believe the new location for the referenced document is http://leepoint.net/data/expressions/22compareobjects.html
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the update
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!