8

A coworker and I had a discussion about how Java represents enumerations. I was under the impression they were strictly ints like C/C++. Or, if you add behavior (Type-Safe enum), it gets wrapped in a class. He believed that if it's small enough Java would compact it to a byte.

However, I found this on the Oracle site:

Java programming language enums are far more powerful than their counterparts in other languages, which are little more than glorified integers. The new enum declaration defines a full-fledged class (dubbed an enum type).

I take it they are actual objects then. If so, is there a way to optimize them to save space?

Thanks

Edit: As mentioned in a comment on Jon's answer, I am after the serialization size of an Enum.

Tony
  • 2,473
  • 1
  • 21
  • 34
  • 1
    If you need to "optimize Java Enums to save space" then you are doing it wrong. Specifically, if your Enums take up too much space then it seems likely that you have too many Enums. – DwB Sep 01 '11 at 21:58
  • It's not that we have too many enums. It's that we intend on passing objects with enums around. So, the smaller the structure the more efficient it is to send. Or that was the idea... – Tony Sep 01 '11 at 22:04
  • If you are using Enums to define type (for instance, this is a Blammy report so it has type = enum.Blammy) they you might consider polymorphism instead (like, class BlammyReport extends ...). – DwB Sep 01 '11 at 22:06
  • True, but what happens when the objects no longer differ from each other? So a BlammyReport is the same as a TPSReport but have different types and need to be handled differently? – Tony Sep 01 '11 at 22:11
  • Polymorphism for the win! The functionality that is the same can be inplemented in the parent class, but use the class type to facilitate handling differences. – DwB Sep 02 '11 at 12:31
  • I thought about doing that but it seemed silly. – Tony Sep 02 '11 at 13:57

1 Answers1

18

No, Java enum values really are objects. They can have fields, methods etc - and different implementations of methods on a per-value basis. However, there's only a fixed set of them - it's not like you create instances of the enum type yourself; the set of valid values is created at type initialization time. Unless you've got a huge number of enum values, it's highly unlikely you need to even think about optimizing.

Note that one bit of optimization which is easy to achieve is using EnumSet whenever you're logically considering a set of enum values. This uses a bit pattern to basically represent the set efficiently.

(Note that C# is closer to C++ than Java here - the C# enums are sadly non-object-oriented. Sigh.)

EDIT: Enum values are serialized by name according to the documentation:

Support has been added to serialization to handle enumerated types, which are new in version 5.0. The rules for serializing an enum instance differ from those for serializing an "ordinary" serializable object: the serialized form of an enum instance consists only of its enum constant name, along with information identifying its base enum type. Deserialization behavior differs as well--the class information is used to find the appropriate enum class, and the Enum.valueOf method is called with that class and the received constant name in order to obtain the enum constant to return.

If you're really after a small serialized form though, you should probably steer clear of Java's built-in serialization anyway, which is relatively verbose (as well as being extremely sensitive to versioning issues). There are all kinds of alternatives - the one I know best is Protocol Buffers, which does serialize enum values as just integers.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Here's a quote from Josuha Bloch "On the surface, these enum types may appear similar to those of other languages, such as C, C++, and C#...Java's enum types are full-fleged classes" – Preston Sep 01 '11 at 21:52
  • The EnumSet is pretty neat but handles a different problem. The space concern we have is more related to serializing the smallest object possible. However, it seems to be a moot point if we strip the enum down to bare-bones. – Tony Sep 01 '11 at 22:09
  • @Tony: You should have mentioned you were after *serialization* size - I have a sneaking suspicion that enum values are serialized to their ordinal value anyway. – Jon Skeet Sep 01 '11 at 22:11
  • @Tony: Assuming you deliberately unaccepted the answer, what else are you looking for? – Jon Skeet Sep 01 '11 at 22:12
  • @Jon Updated question. I realized as I read your answer that I should have clarified that. Thanks – Tony Sep 01 '11 at 22:13
  • @Jon Proof they serialize to ordinal size would be definitive. – Tony Sep 01 '11 at 22:15
  • 1
    @Tony: I've updated my answer. It turns out the serialization is by name rather than ordinal value (see referenced documentation) - but if you're using Java's binary serialization and size is a significant consideration, you should think about alternatives anyway. – Jon Skeet Sep 01 '11 at 22:16
  • Wow, that is an answer. Thanks a lot! – Tony Sep 01 '11 at 22:16