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

查看:38
本文介绍了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 结构的 16 位 constant_pool_count 字段限制为 65535 个条目(第 4.1 节).这是对单个类或接口的总复杂性的内部限制.

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 字节,受代码属性(第 4.7.3 节)和 LineNumberTable 属性(第 4.7.8 节)的异常表中索引的大小限制) 和 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天全站免登陆