Varargs and the argument '...'

Consider a method declaration:

String.format(String, Object ...) 

The Object ... argument is just a reference to an array of Object s. Is there a way to use this method with reference to the actual array of Object ? . If I pass an array of Object into an argument ... - will the resulting value of the argument be a two-dimensional array - because Object[] itself is an Object :

 Object[] params = ....; // Make the array (for example based on user-input) String s = String.format("%S has %.2f euros", params); 

So, the first component of the array (which is used in the String.format method) will be an array, and it will generate:

 [class.getName() + "@" + Integer.toHexString(hashCode())] 

and then an error, since the size of the array is 1.

The bold line is the real question.
This is the second question: Does the array / parameter ... name?

+42
java arrays parameters variadic-functions
Nov 01 '09 at 11:11
source share
3 answers

From docs to varargs :

Three periods after the final parameter type indicate that the final argument can be passed as an array or as a sequence of arguments.

So you can pass multiple arguments or an array.

The following works fine:

 class VarargTest { public static void main(String[] args) { Object[] params = {"x", 1.2345f}; String s = String.format("%s is %.2f", params); System.out.println(s); // Output is: x is 1.23 } } 
+51
Nov 01 '09 at 11:20
source share

You can just pass an array:

 public void foo(String... args) { } String args[] = new String[10]; foo(args); 
+11
Nov 01 '09 at 11:16
source share

The situation you are describing will be quite rare: in most cases your varargs elements will be String s, or numbers, or Widget s ... it will be unusual for them to be Object (this can be anything) or arrays.

But if the varargs argument has a bunch of Object or an array type, then your question arises: you can pass one array to it, and then how does the compiler know if you mean to pass an array (the one you provided) or a series of 1 element that should it insert into an array for you?

A quick test shows the answer:

 public class TestClass { public static void main(String[] args) { Object anObject = new Object(); Object[] anArray = new Object[] {anObject, anObject}; System.out.println("object1 = " + anObject); System.out.println("array1 = " + anArray); takesArgs(); takesArgs(anObject, anObject); takesArgs(anArray); // is this the same as array1? takesArgs(anArray, anArray); } public static void takesArgs(Object... stuff) { System.out.println("The array was " + stuff); } } 

The result of the execution (your exact numbers will vary:

 object1 = java.lang.Object@3e25a5 array1 = [Ljava.lang.Object;@19821f The array was [Ljava.lang.Object;@addbf1 The array was [Ljava.lang.Object;@42e816 The array was [Ljava.lang.Object;@19821f The array was [Ljava.lang.Object;@9304b1 

Thus, the answer is that in ambiguous cases, it considers what you passed as an array, instead of creating a new array to transfer it. This makes sense, as you can always wrap it in an array yourself if you want a different interpretation.

+8
Nov 03 '09 at 1:27
source share



All Articles