How many parameters are available for varargs object in java?

I am interested to know how many parameters you can pass using the varargs facility in Java.

Is there a JVM or memory limit? As far as I understand, varargs is implemented as an array, so the limit is determined by the amount of memory. Is it correct?

+3


source to share


3 answers


Yes there is. Try to allocate memory that is larger than your Pergemn memory that is throwing away OutOfMemory

.

Note that there is no information about array or varargs here. You are free to use your memory under the memory you allocated.



I hope this is just for teaching and not for using these many in your application.

+2


source


Varargs is syntactic sugar for an on-the-fly array that is large enough to fit the encoded elements.

The limit will then be the size of the array, which is 2 31 (i.e. large).

Considering the size is determined by the actual number of parameters encoded, you never have to worry about that.




A quick test confirms that the normal method parameter 255 does not apply to individual varargs.

+3


source


There is no explicit limit to the number of arguments for the arity variable. However, the size of the method is limited. It should not exceed 65535 byte codes .

When javac

compiles the call to the arity variable method, it creates and populates the array using a template like:

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

      

So, filling one element of the array takes 5 or 6 byte codes.
This means that you can actually call the method with just over 10K arguments .

+2


source







All Articles