What is the maximum number of parameters that a method in Java can have and why?
I am using Java 1.8 on a 64-bit Windows system.
All the answers on StackOverflow about this say that the technical limit is 255 parameters without specifying why.
To be precise, 255 for static and 254 for non-static (this
will be the 255th in this case) methods.
I thought this could be described in some sort of specification and that there is simply a statically defined maximum number of parameters allowed.
But this was only valid for int
and all 4-bytes types.
I did some tests with long
parameters, and I was only able to declare 127 parameters in that case.
With String
parameters, the allowed number I deduced from testing is 255 (it may be because the reference size is 4 bytes in Java?).
But since I am using a 64-bit system, references size should be 8 bytes wide and so with String
parameters the maximum allowed number should be 127, similar to long
types.
How does this limit is exactly applied?
Does the limit have anything to do with the stack size of the method?
Note: I am not really going to use these many parameters in any method, but this question is only to clarify the exact behavior.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…