What is the maximum of number of arguments for varargs in java?
Asked Answered
S

1

23

What is the maximum of number of arguments which can be used by a vararg in java ? I believe there should be some limit and it is not infinite.

Say answered 10/8, 2013 at 16:55 Comment(4)
If you're ever passing anywhere close to that number of parameters (around Integer.MAX_VALUE) via var-args, then you're doing something wrong.Medicate
@berry120 Although it is a somewhat theoretical question, it is not uninteresting.Carbonation
Note that all answers that tell a number greater 64K are wrong, no matter how many upvotes they got.Johnstone
@Johnstone Which answers? ;-)Womanly
J
37

A method (including the static class initializer) can have at most 64k. If the arguments are such that they can be pushed with a single bytecode that is 1 byte long each, you can have something about 64000 arguments on a call.

Johnstone answered 10/8, 2013 at 17:12 Comment(11)
Ah! I didn't knew of that. Didn't took into consideration the method size actually. But I can't delete my answer, as it is accepted. :( Still I'll give you a +1. Thanks, I learnt something new. :)Sanitary
@RohitJain No issues. I actually ran into this when I once tried to make a huge static array containing a parser table. I had to split the array initialization in different static initialization methods, but of course you can't split a method invocation....Johnstone
and what about main(String... params)? can i pass more parameters from the command line?Manakin
@Manakin Probably yes, unless your shell doesn't support it. Nowadays the limits are loose, but I remember times when the usual space for command line arguments was 4k. But then it is no Java issue anymore in a strict sense. Should also be easy to test, just run a shell script that creates 100,000 files or so, and then run your java prog with argument *Johnstone
@Johnstone "at most 64k" – can you elaborate? Is that some JVM specification limit or otherwise specified?Womanly
MC emperor yes it is.Johnstone
@Johnstone Could you provide a link to the specs?Womanly
MC Emperor see the JVM spec, obtainable on the web.Johnstone
A vararg is one array as an argument.Competency
@PeterLawrey are you sure you understood the question and my answer?Johnstone
By the way, variable arity parameter seems to be the technical name for varargs in the Java SE Specifications. But I could not find any details about limits.Enrol

© 2022 - 2024 — McMap. All rights reserved.