22

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.

Ingo
  • 36,037
  • 5
  • 53
  • 100
ACS
  • 347
  • 4
  • 9

1 Answers1

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
  • 2
    and 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
  • MC emperor yes it is. – Ingo Jan 06 '17 at 13:49
  • @Ingo Could you provide a link to the specs? – MC Emperor Jan 06 '17 at 14:37
  • MC Emperor see the JVM spec, obtainable on the web. – Ingo Jan 06 '17 at 19:15
  • A vararg is one array as an argument. – Peter Lawrey Jun 27 '18 at 15:37
  • @PeterLawrey are you sure you understood the question and my answer? – Ingo Jun 30 '18 at 09:07