is it possible to create a function in java that supports any number of parameters and then to be able to iterate through each of the parameter provided to the function ?
thanks
kfir
When you call a function in JavaScript, you can pass in any number of arguments, regardless of what the function declaration specifies. There is no function parameter limit. In the above function, if we pass any number of arguments, the result is always the same because it will take the first two parameters only.
There is a technical maximum of 255 parameters that a method can have.
You can define as many parameters as you may need, but too many of them will make your routine difficult to understand and maintain.
Except for functions with variable-length argument lists, the number of arguments in a function call must be the same as the number of parameters in the function definition. This number can be zero. The maximum number of arguments (and corresponding parameters) is 253 for a single function.
Java has had varargs since Java 1.5 (released September 2004).
A simple example looks like this...
public void func(String ... strings) { for (String s : strings) System.out.println(s); }
Note that if you wanted to require that some minimal number of arguments has to be passed to a function, while still allowing for variable arguments, you should do something like this. For example, if you had a function that needed at least one string, and then a variable length argument list:
public void func2(String s1, String ... strings) { }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With