什么是Java编译枚举? [英] What does Java compile an enumeration down to?

查看:194
本文介绍了什么是Java编译枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

同事和我讨论了Java如何表示枚举。我的印象是,他们严格的ints像C / C ++。或者,如果添加行为(类型安全枚举),它会包装在类中。但是,我在Oracle网站上发现了这一点:

Java编程语言枚举比其他语言中的对应函数强大得多,它们只是荣耀的整数。新的枚举声明定义了一个完整的类(被称为枚举类型)。

我认为它们是实际的对象。



感谢



编辑:如上所述

解决方案

不,Java枚举值真的是对象。它们可以具有字段,方法等 - 以及基于每值的方法的不同实现。然而,只有一组固定的 - 它不是你自己创建枚举类型的实例;在类型初始化时创建有效值集合。除非您拥有大量的枚举值,否则您甚至不需要考虑优化。



请注意,一位可以容易地实现的优化是使用 EnumSet ,只要逻辑上考虑一组枚举值。



(注意C#比Java更接近于C ++ - C#枚举很可能是非面向对象的。 Sigh。)



编辑:根据名称 javase / 1,5.0 / docs / guide / serialization / relnotes15.html>文档


已添加支持到序列化以处理枚举类型,这在5.0版本中是新的。串行化枚举实例的规则与序列化普通可序列化对象的规则不同:枚举实例的序列化形式仅包含枚举常量名称以及标识其基本枚举类型的信息。反序列化行为也不同 - 类信息用于查找适当的枚举类,并且使用该类和接收的常量名称调用Enum.valueOf方法以获取要返回的枚举常量。


如果你真的是一个小的序列化形式,你应该尽可能避开Java的内置序列化,这是相对冗长的因为对版本问题非常敏感)。有各种各样的选择 - 我最熟悉的一个是协议缓冲区 将枚举值序列化为整数。


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.

解决方案

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.

这篇关于什么是Java编译枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆