Java中枚举元素的最大数量 [英] Maximum number of enum elements in Java
问题描述
我想了解一个switch语句中最大的个案数量。由于switch中允许的最大的原始类型是int,因此我们有-2,147,483,648到2,147,483,647的情况,一个是默认的情况。但是枚举也被允许...所以问题..
从类文件格式规格:
每个类或每个接口常量池由ClassFile结构的16位constant_pool_count字段限制为65535个条目(§4.1)。这对单一类或接口的整体复杂性构成内部限制。
我相信这意味着你不能有更多的那么65535在单个类中命名为东西,这也将限制枚举常量的数量。
如果看到一个带有2幸运的是,这不可能发生:
$ b
每个非本机非抽象方法的代码量通过Code属性的exception_table中的索引大小限制为65536个字节(§4.7 .3),LineNumberTable属性(§4.7.8)和LocalVariableTable属性(§4.7.9)中。
What is the maximum number of elements allowed in an enum in Java?
I wanted to find out the maximum number of cases in a switch statement. Since the largest primitive type allowed in switch is int, we have cases from -2,147,483,648 to 2,147,483,647 and one default case. However enums are also allowed... so the question..
解决方案From the class file format spec:
The per-class or per-interface constant pool is limited to 65535 entries by the 16-bit constant_pool_count field of the ClassFile structure (§4.1). This acts as an internal limit on the total complexity of a single class or interface.
I believe that this implies that you cannot have more then 65535 named "things" in a single class, which would also limit the number of enum constants.
If a see a switch with 2 billion cases, I'll probably kill anyone that has touched that code.
Fortunately, that cannot happen:
The amount of code per non-native, non-abstract method is limited to 65536 bytes by the sizes of the indices in the exception_table of the Code attribute (§4.7.3), in the LineNumberTable attribute (§4.7.8), and in the LocalVariableTable attribute (§4.7.9).
这篇关于Java中枚举元素的最大数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!