*
The moose likes Beginning Java and the fly likes Enum vs Long Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Enum vs Long" Watch "Enum vs Long" New topic
Author

Enum vs Long

Sandeep Jindal
Ranch Hand

Joined: Aug 25, 2003
Posts: 180
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.


SCJP 5.0
http://sites.google.com/site/duddlutechnologies/home
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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!).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Sandeep Jindal
Ranch Hand

Joined: Aug 25, 2003
Posts: 180
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

Joined: Dec 30, 2009
Posts: 46
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Enum vs Long
 
Similar Threads
Enums with numbers
Issue with assigning enum instance variable
Can we support Map/HashMap in the webservice as one of internal parameters?
Sorting a List of Value Objects
help with logic:iterate struts tag