如何在内部排列或链接枚举元素? [英] How Enum elements are arranged or linked internally?
问题描述
数据类型
,通过查看数据类型字,我得到了几个问题,例如内存中是否有预定义的内存大小,或者是枚举
设置为任何数据结构
像队列
用于快速访问 根据Java文档
枚举声明定义一个类(称为枚举类型)。枚举类体
。
可以包括方法和其他字段。编译器自动添加一些特殊的
方法,当它创建一个枚举
为什么编译器为枚举添加特殊方法。有人可以点亮这个吗?
提前感谢
为什么编译器为枚举添加特殊方法。
基本上,您可以轻松获取枚举类型的值。
唯一额外的方法是 valueOf
和值
。此外,还有一个用于每个类型的值的公共静态final 字段,以便您可以实际访问该类型的约束条件值。
想象一下,编译器没有添加 valueOf
和值
方法(或至少值
方法) - 你将如何访问枚举类型的所有值?这通常是一件有用的事情。
现在 EnumSet
提供了类似的功能,但是要获得所有值,您必须提供一个 class<>
引用,这可能是一个痛苦。 值
方法稍微调用一点(尽管也可能更贵)。
为了回答这些值的存储方式,指定每个值都有一个静态字段,执行细节,Oracle Java编译器还包括一个带有数组的静态字段,该数组基本上克隆在值
方法中。
您可以自己看到:
枚举Foo {
BAR,BAZ ;
}
编译:
javac Foo.java
解码:
javap -private Foo
从Foo.java编译
final class Foo extends java.lang。枚举<富> {
public static final Foo BAR;
public static final Foo BAZ;
private static final Foo [] $ VALUES;
public static Foo [] values();
public static Foo valueOf(java.lang.String);
private Foo();
static {};
}
如果您使用 javap -c
你会看到 values()
基本上是:
public static Foo [] values(){
return(Foo [])$ VALUES.clone();
}
静态初始化程序块创建有限的一组实例并设置字段。 / p>
In Java Enum is a special kind of datatype
, by looking at the word datatype I got few questions like does it have predefined memory size in memory or Are the elements in Enum
set to any data structure
like Queue
for fast access.
As per Java doc
The enum declaration defines a class (called an enum type). The enum class body
can include methods and other fields. The compiler automatically adds some special
methods when it creates an enum
.
Why the compiler adds special methods to enum. Could anybody shed light on this?
Thanks in advance?
Why the compiler adds special methods to enum.
So that you can easily get at the values of the enum type, basically.
The only extra methods are valueOf
and values
. In addition, there are public static final fields, one for each value of the type - again, so that you can actually access the constrainted set of values of that type.
Imagine that the compiler didn't add valueOf
and values
methods (or at least the values
method) - how would you access "all the values of the enum type"? It's often a useful thing to do.
Now EnumSet
provides similar facilities, but to get "all the values" you have to provide a Class<>
reference, which can be a pain. The values
method is somewhat simpler to invoke (although potentially more expensive as well).
See the JLS section 8.9 for more details of exactly what's provided.
To answer the matter of how the values are stored, it's specified that there's a static field per value, but as an implementation detail the Oracle Java compiler also includes a static field with an array - that array is basically cloned in the values
method.
You can see this for yourself:
enum Foo {
BAR, BAZ;
}
Compile:
javac Foo.java
Decompile:
javap -private Foo
Compiled from "Foo.java"
final class Foo extends java.lang.Enum<Foo> {
public static final Foo BAR;
public static final Foo BAZ;
private static final Foo[] $VALUES;
public static Foo[] values();
public static Foo valueOf(java.lang.String);
private Foo();
static {};
}
If you use javap -c
you'll see that values()
is basically:
public static Foo[] values() {
return (Foo[]) $VALUES.clone();
}
The static initializer block creates the limited set of instances and sets the fields.
这篇关于如何在内部排列或链接枚举元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!