Java中的最大枚举元素数 [英] Maximum number of enum elements in Java

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

问题描述

Java 中枚举中允许的最大元素数是多少?

What is the maximum number of elements allowed in an enum in Java?

我想找出 switch 语句中的最大 case 数.由于 switch 中允许的最大原始类型是 int,因此我们有从 -2,147,483,648 到 2,147,483,647 的情况和一种默认情况.但是也允许使用枚举...所以问题..

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..

推荐答案

来自 类文件格式规范:

ClassFile 结构(第 4.1 节)的 16 位 constant_pool_count 字段将每个类或每个接口的常量池限制为 65535 个条目.这作为对单个类或接口的总复杂性的内部限制.

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.

我相信这意味着在一个类中不能有超过 65535 个命名的事物",这也会限制枚举常量的数量.

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.

如果看到一个有 20 亿个案例的开关,我可能会杀死任何接触过该代码的人.

If a see a switch with 2 billion cases, I'll probably kill anyone that has touched that code.

幸运的是,这不可能发生:

Fortunately, that cannot happen:

每个非本地、非抽象方法的代码量被限制为 65536 字节,这取决于 Code 属性 (§4.7.3) 和 LineNumberTable 属性 (§4.7.8) 中的 exception_table 中的索引大小) 和 LocalVariableTable 属性 (§4.7.9).

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屋!

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