File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Enum vs Long" Watch "Enum vs Long" New topic

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
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15080

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 8 API documentation
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:
subject: Enum vs Long
It's not a secret anymore!