简体   繁体   中英

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.

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

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

The limit then would be the size of an array, which is 2 31 (ie large).

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.

Yes there is. Try to allocate the memory which is greater than your Pergemn memory, that throws 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. However the method size is limited. It should not exceed 65535 bytecodes .

When javac compiles a call to a variable arity method, it creates and fills the array using the pattern like:

+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.
This means you can practically call a method with a little more than 10K arguments .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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