Java 方法声明中的最大参数数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30581531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Maximum number of parameters in Java method declaration
提问by userv
What is the maximum number of parameters that a method in Java can have and why?
Java 中的方法可以拥有的最大参数数量是多少?为什么?
I am using Java 1.8 on a 64-bit Windows system.
我在 64 位 Windows 系统上使用 Java 1.8。
All the answers on StackOverflow about this say that the technical limit is 255 parameters without specifying why.
StackOverflow 上关于这个的所有答案都说技术限制是 255 个参数,但没有说明原因。
To be precise, 255 for static and 254 for non-static (this
will be the 255th in this case) methods.
准确地说,静态方法为 255,非静态方法为 254(this
在这种情况下将是第 255 个)方法。
I thought this could be described in some sort of specification and that there is simply a statically defined maximum number of parameters allowed.
我认为这可以在某种规范中进行描述,并且只允许静态定义的最大参数数量。
But this was only valid for int
and all 4-bytes types.
I did some tests with long
parameters, and I was only able to declare 127 parameters in that case.
但这仅对int
所有 4 字节类型有效。我用long
参数做了一些测试,在那种情况下我只能声明 127 个参数。
With String
parameters, the allowed number I deduced from testing is 255 (it may be because the reference size is 4 bytes in Java?).
使用String
参数,我从测试中推导出的允许数字是 255(可能是因为 Java 中的引用大小是 4 个字节?)。
But since I am using a 64-bit system, references size should be 8 bytes wide and so with String
parameters the maximum allowed number should be 127, similar to long
types.
但是因为我使用的是 64 位系统,引用大小应该是 8 字节宽,所以String
参数的最大允许数应该是 127,类似于long
类型。
How does this limit is exactly applied?
这个限制是如何准确应用的?
Does the limit have anything to do with the stack sizeof the method?
限制是否与方法的堆栈大小有关?
Note: I am not really going to use these many parameters in any method, but this question is only to clarify the exact behavior.
注意:我真的不打算在任何方法中使用这么多参数,但这个问题只是为了澄清确切的行为。
采纳答案by Umberto Raimondi
That limit is defined in the JVM Specification:
该限制在JVM 规范中定义:
The number of method parameters is limited to 255by the definition of a method descriptor (§4.3.3), where the limit includes one unit for thisin the case of instance or interface method invocations.
的方法的参数的数量被限制为255通过方法描述符(§4.3.3),其中该限制包括用于一个单位的定义这在实例或接口方法调用的情况下。
Section §4.3.3gives some additional information:
第4.3.3节提供了一些附加信息:
A method descriptor is valid only if it represents method parameters with a total length of 255 or less, where that length includes the contribution for thisin the case of instance or interface method invocations.
The total length is calculated by summing the contributions of the individual parameters, where a parameter of type long or double contributes two units to the length and a parameter of any other type contributes one unit.
方法描述符仅在它表示总长度为 255 或更少的方法参数时才有效,其中该长度包括在实例或接口方法调用的情况下对此的贡献。
总长度是通过对各个参数的贡献求和来计算的,其中 long 或 double 类型的参数对长度贡献两个单位,而任何其他类型的参数贡献一个单位。
Your observations were spot on, double word primitives (long
/double
) need twice the size of usual 4 bytes variables and 4 bytes object instance references.
您的观察很准确,双字原语 ( long
/ double
) 需要两倍于通常 4 字节变量和 4 字节对象实例引用的大小。
Regarding the last part of your question related to 64bit systems, the specification defines how many units a parameter contribute, that part of the specification must still be complied witheven on a 64bit platform, the 64bit JVM will accomodate 255 instance parameters (like your 255 Strings
) regardless of the internal object's pointer size.
关于您与 64 位系统相关的问题的最后一部分,规范定义了参数贡献的单位数,即使在 64 位平台上也必须遵守该部分规范,64 位 JVM 将容纳 255 个实例参数(例如您的 255 Strings
) 不管内部对象的指针大小。
回答by Vivin Paliath
Section 4.3.3of the JVM specification has the information you are looking for:
JVM 规范的第 4.3.3 节包含您正在寻找的信息:
A method descriptor is valid only if it represents method parameters with a total length of 255 or less, where that length includes the contribution for this in the case of instance or interface method invocations. The total length is calculated by summing the contributions of the individual parameters, where a parameter oftype long or double contributes two unitsto the length and aparameter of any other type contributes one unit.
方法描述符仅在它表示总长度为 255 或更少的方法参数时才有效,其中该长度包括在实例或接口方法调用的情况下对此的贡献。总长度是通过对各个参数的贡献求和来计算的,其中long 或 double 类型的参数对长度贡献两个单位,任何其他类型的参数贡献一个单位。
Therefore it appears that whether the host-machine is 32-bit or 64-bit has no effect on the number of parameters. If you notice, the documentation speaks in terms of "units", where the length of one "unit" is a function of the word-size. If the number of parameters directly proportional to word-size, there would be portability issues; you wouldn't be able to compile the same Java program on different architectures (assuming that at least one method used the maximum-number of parameters on the architecture with the larger word-size).
因此,主机是32位还是64位似乎对参数数量没有影响。如果您注意到,文档是用“单位”来表示的,其中一个“单位”的长度是字长的函数。如果参数个数与word-size成正比,就会出现可移植性问题;您将无法在不同的体系结构上编译相同的 Java 程序(假设至少一种方法在具有较大字长的体系结构上使用了最大数量的参数)。
回答by Matthew Brzezinski
I found an interesting issue from a newsletter about this, http://www.javaspecialists.eu/archive/Issue059.html
我从有关此的时事通讯中发现了一个有趣的问题,http://www.javaspecialists.eu/archive/Issue059.html
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. This acts as an internal limit on the total complexity of a single class or interface. 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, in the LineNumberTable attribute, and in the LocalVariableTable attribute.
The greatest number of local variables in the local variables array of a frame created upon invocation of a method is limited to 65535 by the size of the max_locals item of the Code attribute giving the code of the method. Note that values of type long and double are each considered to reserve two local variables and contribute two units toward the max_locals value, so use of local variables of those types further reduces this limit.
The number of fields that may be declared by a class or interface is limited to 65535 by the size of the fields_count item of the ClassFile structure. Note that the value of the fields_count item of the ClassFile structure does not include fields that are inherited from superclasses or superinterfaces.
ClassFile 结构的 16 位 constant_pool_count 字段将每个类或每个接口的常量池限制为 65535 个条目。这是对单个类或接口的总复杂性的内部限制。每个非本机、非抽象方法的代码量受 Code 属性的 exception_table、LineNumberTable 属性和 LocalVariableTable 属性中的索引大小限制为 65536 字节。
在调用方法时创建的框架的局部变量数组中的局部变量的最大数量限制为 65535,这取决于给出方法代码的 Code 属性的 max_locals 项的大小。请注意,long 和double 类型的值均被视为保留两个局部变量并为 max_locals 值贡献两个单位,因此使用这些类型的局部变量进一步降低了此限制。
类或接口可以声明的字段数被 ClassFile 结构的 fields_count 项的大小限制为 65535。请注意,ClassFile 结构的 fields_count 项的值不包括从超类或超接口继承的字段。