How does the Java array argument declaration syntax ... work?
Check out the Java Language Specification, Third Edition, Chapter 8 (Classes). Buried in there is this nugget:
If the last formal parameter is a variable arity parameter of type T, it is considered to define a formal parameter of type T[]. The method is then a variable arity method. Otherwise, it is a fixed arity method. Invocations of a variable arity method may contain more actual argument expressions than formal parameters. All the actual argument expressions that do not correspond to the formal parameters preceding the variable arity parameter will be evaluated and the results stored into an array that will be passed to the method invocation (§15.12.4.2).
Basically, the last parameter of any method call can have T...
. If it has that, it is converted to T[]
.
So basically, what you have is a fancy way of reproducing the more traditional
String[] args
What does a ... do as a parameter of a java function?
I is the syntax for specifying varargs i.e. specifying that a method can take a variable number of arguments.
The three periods after the final parameter's type indicate that the final argument may be passed as an array or as a sequence of arguments. Varargs can be used only in the final argument position.
How do I declare and initialize an array in Java?
You can either use array declaration or array literal (but only when you declare and affect the variable right away, array literals cannot be used for re-assigning an array).
For primitive types:
int[] myIntArray = new int[3]; // each element of the array is initialised to 0
int[] myIntArray = {1, 2, 3};
int[] myIntArray = new int[]{1, 2, 3};
// Since Java 8. Doc of IntStream: https://docs.oracle.com/javase/8/docs/api/java/util/stream/IntStream.html
int [] myIntArray = IntStream.range(0, 100).toArray(); // From 0 to 99
int [] myIntArray = IntStream.rangeClosed(0, 100).toArray(); // From 0 to 100
int [] myIntArray = IntStream.of(12,25,36,85,28,96,47).toArray(); // The order is preserved.
int [] myIntArray = IntStream.of(12,25,36,85,28,96,47).sorted().toArray(); // Sort
For classes, for example String
, it's the same:
String[] myStringArray = new String[3]; // each element is initialised to null
String[] myStringArray = {"a", "b", "c"};
String[] myStringArray = new String[]{"a", "b", "c"};
The third way of initializing is useful when you declare an array first and then initialize it, pass an array as a function argument, or return an array. The explicit type is required.
String[] myStringArray;
myStringArray = new String[]{"a", "b", "c"};
Java Pass Array of Arguments
You can do this with reflection, but it's probably more performant to use cached method handles that you initialize once, then reuse as many times as you need.
class SpreadInvoker {
public static void draw1(int x, String y) {
System.out.printf("draw1(%s, %s)%n", x, y);
}
public void draw2(int x, int y) {
System.out.printf("draw2(%s, %s)%n", x, y);
}
static MethodHandle DRAW1;
static MethodHandle DRAW2;
public static void main(String[] args) throws Throwable {
DRAW1 = MethodHandles.lookup()
.findStatic(
SpreadInvoker.class,
"draw1",
MethodType.methodType(
void.class,
int.class,
String.class
)
)
.asSpreader(Object[].class, 2);
DRAW2 = MethodHandles.lookup()
.findVirtual(
SpreadInvoker.class,
"draw2",
MethodType.methodType(
void.class,
int.class,
int.class
)
).asSpreader(Object[].class, 2);
SpreadInvoker instance = new SpreadInvoker();
final Object[] args1 = { 13, "twenty-six" };
final Object[] args2 = { 13, 26 };
DRAW1.invoke(args1); // SpreadInvoker.draw1(13, "twenty-six")
DRAW2.invoke(instance, args2); // instance.draw2(13, 26)
}
}
Output:
draw1(13, twenty-six)
draw2(13, 26)
Note, however, that this is the sort of thing you'd do if you don't know what method you need to call at compile time. This sort of thing should almost never be necessary.
Java array initialization within argument list
You can only use the { "hello", "world" }
initialization notation when declaring an array variable or in an array creation expression such as new String[] { ... }
.
See Section 10.6 Array Initializers in the Java Language Specification:
An array initializer may be specified in a declaration, or as part of an array creation expression (§15.10), creating an array and providing some initial values
Related Topics
Soap Request to Webservice with Java
Multiple Inheritance for an Anonymous Class
Numeric Textfield for Integers in Javafx 8 with Textformatter And/Or Unaryoperator
How to Find Out If "Debug Mode" Is Enabled
Java User.Home Is Being Set to %Userprofile% and Not Being Resolved
Iteratively Compute the Cartesian Product of an Arbitrary Number of Sets
Java Gif Animation Not Repainting Correctly
Maven Dependency for Servlet 3.0 API
In Java Critical Sections, What Should I Synchronize On
Capturing Browser Logs with Selenium Webdriver Using Java
Totally Confused with Java.Exe
What Does Super.Paintcomponent(G) Do
Parsing Nested JSON Data Using Gson
How to Use 3Rd Party Library in Java9 Module
Method Calls Inside a Java Class Return an "Identifier Expected After This Token" Error