• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enum vs Long

 
Sandeep Jindal
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

A very basic question. I have a VO, where in for one member variable I have option of storing either:
a) a long value, id
b) Enum


MY VO could be:



In general which one is better?

Or side questions could be:
If we need to compare long value (e.g. long1 == long2) and navType.FINAL.equals(NAVType.Final), which one is better?

I do understand, that memory footprints of using Enum is better than using Long.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using an enum is more type safe.

In a long, you can store any value that fits into the long. It's easy to make a mistake and store a value that has no meaning in your application. An enum can only take valid values.

If you need to store and retrieve the data to and from an external system (for example a database), using an enum can be a little bit more work, because you need to convert the enum to and from a format that the external system understands.

I'd use an enum in this case, because of the type safety, and not because the memory footprint is better (which I doubt is true!).
 
Sandeep Jindal
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reponse! That answers: Using Enum is better because it is type safe. Using Enum is not better because we need to do little extra coding.

Are there any further reasons/comparison between these two. E.g. Memory Footprint, Performance?

 
pete reisinger
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it depends on the situation.
If you have fixed number of ids, then I would use enum for sure, because you have more options with enum. you can add methods etc.
If you can have any id (autogenerated from db) then you cannot use enum.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic