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.
Asked
Active
Viewed 9,487 times
22
-
4If you're ever passing anywhere close to that number of parameters (around `Integer.MAX_VALUE`) via var-args, then you're doing something wrong. – Michael Berry Aug 10 '13 at 16:57
-
3@berry120 Although it is a somewhat theoretical question, it is not uninteresting. – assylias Aug 10 '13 at 17:12
-
3Note that all answers that tell a number greater 64K are wrong, no matter how many upvotes they got. – Ingo Aug 10 '13 at 17:16
-
1@Ingo Which answers? ;-) – MC Emperor Jan 06 '17 at 13:37
1 Answers
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.

Ingo
- 36,037
- 5
- 53
- 100
-
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. :) – Rohit Jain Aug 11 '13 at 08:01
-
@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.... – Ingo Aug 11 '13 at 13:03
-
2and what about `main(String... params)`? can i pass more parameters from the command line? – piotrek May 24 '14 at 09:38
-
1@piotrek 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 * – Ingo May 24 '14 at 16:59
-
@Ingo "at most 64k" – can you elaborate? Is that some JVM specification limit or otherwise specified? – MC Emperor Jan 06 '17 at 13:39
-
-
-
-
-