• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Enum vs Long

 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic