简体   繁体   English

varargs工具允许在java中使用多少个参数?

[英]How many parameters does the varargs facility permit in java?

I am interested in knowing how many parameters you can pass using the varargs facility in Java. 我很想知道使用Java中的varargs工具可以传递多少参数。

Is there a JVM or Memory limit? 是否存在JVM或内存限制? As far as I can understand it varargs is implemented as an array so the limit is determined by the amount of memory. 据我所知,varargs是作为一个数组实现的,所以限制是由内存量决定的。 Is this correct? 它是否正确?

Varargs is syntactic sugar for an on-the-fly array that is just large enough to fit the elements coded. Varargs是用于动态数组的语法糖,其大小足以适合编码的元素。

The limit then would be the size of an array, which is 2 31 (ie large). 那么限制将是数组的大小,即2 31 (即大)。

Considering the size is specified by the actual number of parameters coded, you will never have to worry about it. 考虑到大小是由编码的实际参数数量指定的,您将永远不必担心它。


A quick test confirms that the usual method parameter count of 255 does not apply to individual varargs elements. 快速测试确认通常的方法参数计数255不适用于单个varargs元素。

Yes there is. 就在这里。 Try to allocate the memory which is greater than your Pergemn memory, that throws OutOfMemory . 尝试分配大于Pergemn内存的内存,抛出OutOfMemory

note that there is no speciality about array or varargs here. 请注意,这里没有关于数组或变量的特性。 You can freely use your memory below the memory you allocated. 您可以在分配的内存下方自由使用内存。

I hope this is just for learning and not using those many in your application. 我希望这只是为了学习,而不是在你的应用程序中使用那些。

There is no explicit limit for the number of variable arity arguments. 变量arity参数的数量没有明确的限制。 However the method size is limited. 但是方法的大小是有限的。 It should not exceed 65535 bytecodes . 它不应超过65535字节码

When javac compiles a call to a variable arity method, it creates and fills the array using the pattern like: javac编译对变量arity方法的调用时,它使用如下模式创建并填充数组:

+0: dup
+1: bipush <index> (or sipush if index > 127)
+3: iconst_0
+4: iastore
+5: dup
    ...

So, filling one array element takes 5 or 6 bytecodes. 因此,填充一个数组元素需要5或6个字节码。
This means you can practically call a method with a little more than 10K arguments . 这意味着您几乎可以使用超过10K的参数调用方法。

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

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