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

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

问题描述

Java 中的方法最多可以有多少个参数,为什么?

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

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

准确地说,静态方法为 255,非静态方法为 254(在这种情况下,this 将是第 255 个)方法.

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

但这仅对 int 和所有 4 字节类型 有效.我用 long 参数做了一些测试,在这种情况下我只能声明 127 个参数.

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

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

这个限制是如何应用的?

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

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

解决方案

该限制在 JVM规范:

<块引用>

方法参数的数量限制为 255,根据方法描述符的定义(第 4.3.3 节),其中限制包括一个单位为 this实例或接口方法调用的情况.

§4.3 部分.3 提供了一些附加信息:

<块引用>

方法描述符仅当它表示总长度为 255 或更少的方法参数时才有效,其中该长度包括在实例或接口方法调用的情况下对 this 的贡献.p>

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

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

关于与 64 位系统相关的问题的最后一部分,规范定义了一个参数贡献了多少单位,规范的该部分仍然必须遵守在 64 位平台上,64 位 JVM 将容纳 255 个实例参数(例如您的 255 个 Strings),无论内部对象的指针大小如何.

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天全站免登陆