150

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.

11
  • 41
    7, if you don't want to go insane with readability. (I know what you're actually asking). Commented Jun 1, 2015 at 19:27
  • 15
    I would argue <= 4. Anything more should probably be wrapped up into an object. Commented Jun 1, 2015 at 19:30
  • 5
    Why is this an interesting question? If you're writing a program and you're reaching this limit, then your design is wrong. I don't understand why such a practically useless question gets so many upvotes. Commented Jun 1, 2015 at 20:55
  • 23
    @Jesper because this questions questions knowledge of JVM specification. This question doesn't ask "How to do this or that ?" instead it questions "Why I need to that ?"... +1 Interesting question Userv Commented Jun 2, 2015 at 4:15
  • 3
    @amit exactly what I was thinking. The OP just was curious about it. Commented Jun 2, 2015 at 4:38

3 Answers 3

124

That limit is defined in the JVM Specification:

The number of method parameters is limited to 255 by the definition of a method descriptor (§4.3.3), where the limit includes one unit for this in the case of instance or interface method invocations.

Section §4.3.3 gives some additional information:

A method descriptor is valid only if it represents method parameters with a total length of 255 or less, where that length includes the contribution for this in the case of instance or interface method invocations.

The total length is calculated by summing the contributions of the individual parameters, where a parameter of type long or double contributes two units to the length and a parameter of any other type contributes one unit.

Your observations were spot on, double word primitives (long/double) need twice the size of usual 4 bytes variables and 4 bytes object instance references.

Regarding the last part of your question related to 64bit systems, the specification defines how many units a parameter contribute, that part of the specification must still be complied with even on a 64bit platform, the 64bit JVM will accomodate 255 instance parameters (like your 255 Strings) regardless of the internal object's pointer size.

Sign up to request clarification or add additional context in comments.

14 Comments

I'd add to this answer that on 64 bit architecture the stack is also 64 bit. So, since the limitation on parameter count is stack size bound, 64-bit stack allows storing the same 255 object references. The specific treatment of long and double regardless of system architecture occurs in many places of the spec and appears to be a remnant of 32-bit era.
I was in the process of editing just that part :) Agreed, the spec must still be respected even on different platforms.
Yeah, if the number of parameters was a function of word-size, then that would break portability; you couldn't compile the same Java program successfully on different architectures.
Varargs are turned into an Object array, can be used only one time in the parameter list and occupy the last position. Considering all this, i'd say that using varargs the number of parameters can be "extended" to 254+Integer.MAX_VALUE (at least for the programmer... parameters are still 255), so using that trick you can have Integer.MAX_VALUE object parameters.
@MrTsjolder Take a look at this answer for varargs.
|
12

Section 4.3.3 of the JVM specification has the information you are looking for:

A method descriptor is valid only if it represents method parameters with a total length of 255 or less, where that length includes the contribution for this in the case of instance or interface method invocations. The total length is calculated by summing the contributions of the individual parameters, where a parameter of type long or double contributes two units to the length and a parameter of any other type contributes one unit.

Therefore it appears that whether the host-machine is 32-bit or 64-bit has no effect on the number of parameters. If you notice, the documentation speaks in terms of "units", where the length of one "unit" is a function of the word-size. If the number of parameters directly proportional to word-size, there would be portability issues; you wouldn't be able to compile the same Java program on different architectures (assuming that at least one method used the maximum-number of parameters on the architecture with the larger word-size).

Comments

10

I found an interesting issue from a newsletter about this, http://www.javaspecialists.eu/archive/Issue059.html

The per-class or per-interface constant pool is limited to 65535 entries by the 16-bit constant_pool_count field of the ClassFile structure. This acts as an internal limit on the total complexity of a single class or interface. The amount of code per non-native, non-abstract method is limited to 65536 bytes by the sizes of the indices in the exception_table of the Code attribute, in the LineNumberTable attribute, and in the LocalVariableTable attribute.

The greatest number of local variables in the local variables array of a frame created upon invocation of a method is limited to 65535 by the size of the max_locals item of the Code attribute giving the code of the method. Note that values of type long and double are each considered to reserve two local variables and contribute two units toward the max_locals value, so use of local variables of those types further reduces this limit.

The number of fields that may be declared by a class or interface is limited to 65535 by the size of the fields_count item of the ClassFile structure. Note that the value of the fields_count item of the ClassFile structure does not include fields that are inherited from superclasses or superinterfaces.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.