Basic Java methods with "..." arrays?

Possible duplicate:
What is the ellipsis (...) for in this method signature?
java: how can I create a function that supports any number of parameters?

Well, I'm trying to figure out some examples, and I found such an array definition for arguments in the main method. What is so special about this "..." and what is the difference between a normal string [] args?

thanks

+6
source share
4 answers

This is a Java 5 notation for variable length argument lists. This is roughly equivalent to a String array, but allows you to pass individual parameters that are automatically combined into an array.

void mymethod(String... a) { for (String s : a) { ... } } mymethod("quick", "brown", "fox"); 

This only makes sense when you plan to call a method from your program, so I don’t understand why it would be advisable to use this syntax for your main (). However, it will work.

+7
source

elipsis (...) are varargs. They are used when you want to create a method with any number of arguments. Oracle explains how varargs work in detail here .

+3
source

This is called varargs, which means that any number of arguments can be passed this type ( String ).

1.it should be in the end position

2.It will be processed as an array

+2
source

... used for varargs.

for instance

public void myMethod (Object ... params) {}

The variable parameter is optional and is treated as a null array of objects.

+1
source

Source: https://habr.com/ru/post/914765/


All Articles