• <bdo id='cNF6X'></bdo><ul id='cNF6X'></ul>

    <small id='cNF6X'></small><noframes id='cNF6X'>

  • <legend id='cNF6X'><style id='cNF6X'><dir id='cNF6X'><q id='cNF6X'></q></dir></style></legend>
  • <i id='cNF6X'><tr id='cNF6X'><dt id='cNF6X'><q id='cNF6X'><span id='cNF6X'><b id='cNF6X'><form id='cNF6X'><ins id='cNF6X'></ins><ul id='cNF6X'></ul><sub id='cNF6X'></sub></form><legend id='cNF6X'></legend><bdo id='cNF6X'><pre id='cNF6X'><center id='cNF6X'></center></pre></bdo></b><th id='cNF6X'></th></span></q></dt></tr></i><div id='cNF6X'><tfoot id='cNF6X'></tfoot><dl id='cNF6X'><fieldset id='cNF6X'></fieldset></dl></div>

      1. <tfoot id='cNF6X'></tfoot>

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

        Maximum number of parameters in Java method declaration(Java 方法声明中的最大参数个数)

          • <bdo id='atrQE'></bdo><ul id='atrQE'></ul>

            <small id='atrQE'></small><noframes id='atrQE'>

          • <legend id='atrQE'><style id='atrQE'><dir id='atrQE'><q id='atrQE'></q></dir></style></legend>

            <i id='atrQE'><tr id='atrQE'><dt id='atrQE'><q id='atrQE'><span id='atrQE'><b id='atrQE'><form id='atrQE'><ins id='atrQE'></ins><ul id='atrQE'></ul><sub id='atrQE'></sub></form><legend id='atrQE'></legend><bdo id='atrQE'><pre id='atrQE'><center id='atrQE'></center></pre></bdo></b><th id='atrQE'></th></span></q></dt></tr></i><div id='atrQE'><tfoot id='atrQE'></tfoot><dl id='atrQE'><fieldset id='atrQE'></fieldset></dl></div>

              <tbody id='atrQE'></tbody>
            • <tfoot id='atrQE'></tfoot>

                  本文介绍了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 方法声明中的最大参数个数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Bytecode features not available in the Java language(Java 语言中不可用的字节码功能)
                  ClassCastException because of classloaders?(ClassCastException 因为类加载器?)
                  How can I add a Javaagent to a JVM without stopping the JVM?(如何在不停止 JVM 的情况下将 Javaagent 添加到 JVM?)
                  Cannot load 64-bit SWT libraries on 32-bit JVM ( replacing SWT file )(无法在 32 位 JVM 上加载 64 位 SWT 库(替换 SWT 文件))
                  Encourage the JVM to GC rather than grow the heap?(鼓励 JVM 进行 GC 而不是增加堆?)
                  Why a sawtooth shaped graph?(为什么是锯齿形图形?)
                1. <legend id='EAkcy'><style id='EAkcy'><dir id='EAkcy'><q id='EAkcy'></q></dir></style></legend>

                  <small id='EAkcy'></small><noframes id='EAkcy'>

                    <tbody id='EAkcy'></tbody>

                  <tfoot id='EAkcy'></tfoot>
                    <bdo id='EAkcy'></bdo><ul id='EAkcy'></ul>

                      <i id='EAkcy'><tr id='EAkcy'><dt id='EAkcy'><q id='EAkcy'><span id='EAkcy'><b id='EAkcy'><form id='EAkcy'><ins id='EAkcy'></ins><ul id='EAkcy'></ul><sub id='EAkcy'></sub></form><legend id='EAkcy'></legend><bdo id='EAkcy'><pre id='EAkcy'><center id='EAkcy'></center></pre></bdo></b><th id='EAkcy'></th></span></q></dt></tr></i><div id='EAkcy'><tfoot id='EAkcy'></tfoot><dl id='EAkcy'><fieldset id='EAkcy'></fieldset></dl></div>