简体   繁体   English

Java 方法声明中的最大参数个数

[英]Maximum number of parameters in Java method declaration

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.准确地说,static 为 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 .但这仅对int和所有 4 字节类型有效。 I did some tests with long parameters, and I was only able to declare 127 parameters in that case.我用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 size of 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.注意:我真的不会在任何方法中使用这么多参数,但这个问题只是为了澄清确切的行为。

That limit is defined in the JVM Specification : 该限制在JVM规范中定义:

The number of method parameters is limited to 255 by the definition of a method descriptor (§4.3.3), where the limit includes one unit for this in the case of instance or interface method invocations. 的方法的参数的数量一方法描述符(§4.3.3),其中该限制包括一个单元实例或接口方法调用的情况下的定义限制为255。

Section §4.3.3 gives 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 this in the case of instance or interface method invocations. 方法描述符仅在表示总长度为255或更小的方法参数时才有效,其中该长度包括在实例或接口方法调用的情况下对此的贡献。

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 . 总长度是通过对各个参数的贡献求和来计算的, 其中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 with even 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 )无论内部对象的指针大小如何。

Section 4.3.3 of 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. 方法描述符仅在表示总长度为255或更小的方法参数时才有效,其中该长度包括在实例或接口方法调用的情况下对此的贡献。 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 . 总长度是通过对各个参数的贡献求和来计算的,其中 long或double类型 的参数对 长度 贡献两个单位 而任何其他类型 参数贡献一个单位

Therefore it appears that whether the host-machine is 32-bit or 64-bit has no effect on the number of parameters. 因此,主机是32位还是64位似乎对参数数量没有影响。 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). 您将无法在不同的体系结构上编译相同的Java程序(假设至少有一种方法使用具有较大字长的体系结构上的最大参数数)。

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. ClassFile结构的16位constant_pool_count字段将每类或每接口常量池限制为65535个条目。 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. 每个非本机非抽象方法的代码量由Code属性的exception_table,LineNumberTable属性和LocalVariableTable属性中的索引大小限制为65536字节。

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. 在调用方法时创建的帧的局部变量数组中最大数量的局部变量被Code35属性的max_locals项的大小限制为65535,该属性给出了方法的代码。 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. 请注意,long和double类型的值都被认为是保留两个局部变量并为max_locals值提供两个单位,因此使用这些类型的局部变量会进一步降低此限制。

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. 可以由类或接口声明的字段数量被ClassFile结构的fields_count项的大小限制为65535。 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结构的fields_count项的值不包括从超类或超接口继承的字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM