Java方法声明中的最大参数数 [英] Maximum number of parameters in Java method declaration

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

问题描述

Java中的方法可以拥有的最大参数数量是什么?为什么?



我在64位Windows系统上使用Java 1.8。



StackOverflow上关于此的所有答案都说技术限制是255个参数而没有说明原因。



准确无误,255表示静态,254表示非静态(将是本例中的第255个)方法。



我认为这可以在某种规范中描述,并且只允许静态定义的最大参数数量。



但这仅适用于 int 和所有4字节类型
我用 long 参数进行了一些测试,在这种情况下我只能声明127个参数。



使用 String 参数,我从测试中推导出的允许数量是255(这可能是因为Java中的引用大小是4个字节?)。



但由于我使用的是64位系统,引用大小应为8字节宽,因此使用 String 参数允许的最大数量应该是127,类似于 long 类型。



这个限制是如何应用的?



该限制是否与方法的堆栈大小有关?



<注意:我并不是真的会在任何方法中使用这些参数,但这个问题只是为了澄清确切的行为。

解决方案

该限制在 JVM规范中定义


通过方法描述符的定义,方法参数的数量限制为255 (§ 4.3.3),在实例或接口方法调用的情况下,限制包括 this 的一个单元。


§4.3.3部分提供了一些额外的信息:


方法描述符仅在表示带有tota的方法参数时才有效l长度为255或更小,其中该长度包括在实例或接口方法调用的情况下 this 的贡献。



总长度是通过对各个参数的贡献求和来计算的,其中long或double类型的参数对长度贡献两个单位,参数为任何其他类型贡献一个单位


您的观察结果是双点原语( long / double )需要两倍大小的通常4字节变量和4字节对象实例引用



关于与64位系统相关的问题的最后部分,规范定义了参数贡献的单位数,规范的一部分必须即使在64位平台上,仍然可以遵守,64位JVM将容纳255个实例参数(如255 字符串),无论内部对象的指针大小如何。 / p>

What is the maximum number of parameters that a method in Java can have and why?

I am using Java 1.8 on a 64-bit Windows system.

All the answers on StackOverflow about this say that the technical limit is 255 parameters without specifying why.

To be precise, 255 for static and 254 for non-static (this will be the 255th in this case) methods.

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.

With String parameters, the allowed number I deduced from testing is 255 (it may be because the reference size is 4 bytes in Java?).

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.

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:

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.

Section §4.3.3 gives some additional information:

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 of type long or double contributes two units to the length and a parameter of any other type contributes one unit.

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.

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.

这篇关于Java方法声明中的最大参数数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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