Results 1 to 7 of 7
Thread: the strangeness of Varargs...
- 05-09-2010, 12:18 AM #1
Member
- Join Date
- May 2010
- Location
- Colorado
- Posts
- 6
- Rep Power
- 0
the strangeness of Varargs...
guess the printout after each call to method "print"...
public class TestingVarargs {
static void print(Object...obj){
System.out.println("Object...: " + obj[0]+ " " + obj[1] + " " + obj[2]);
}
public static void main(String[] args){
Object[] myArray1 = {1, "March", 2009};
int[] myArray2 = {9, 1, 1};
print("9", "1", "1");
print(9, 1, 1);
print(new int[] {9, 1, 1}, ", " + 8, 7);
print(myArray1, 9, 2);
print(new Integer[] {9, 1, 1});
print(new String[] {"9", "1", "1"});
print((Object) 9, 1, 1);
print(new Object[] {9, 1, 1});
print(myArray1);
print(myArray2, 9, 3);
print((Object) myArray1, 3, 4);
print(new Object[] {myArray1}, 2, 3);
}
}
-
Is this a question?
Also, please don't multipost the same question multiple times as this unnecessarily fragments the discussion and goes against the users agreement which you signed on joining us. Thanks for your cooperation.
- 05-09-2010, 12:31 AM #3
Once is enough
the strange and weird world of Varargs...
db
- 05-09-2010, 12:33 AM #4
Member
- Join Date
- May 2010
- Location
- Colorado
- Posts
- 6
- Rep Power
- 0
oops... sorry.. was not aware of that :-)
- 05-10-2010, 03:33 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
What, exactly, is odd about that?
It's doing what I would expect...or are you expecting it to identify each individual Object that's passed into it to be checked for whether it is an array or not, and then break that out?
- 05-10-2010, 04:23 PM #6
Member
- Join Date
- May 2010
- Location
- Colorado
- Posts
- 6
- Rep Power
- 0
varargs
well, i guess its confusing exactly how the compiler will interpret the various variations, etc... especially with lines like:
1) print(myArray1);
vs.
2) print(myArray1, 9, 2);
and,
3) print(myArray2);
so if an array of type Object[] is passed by itself (as at "1"), it actually becomes a "non-varargs" call to method "print" (with all the array contents displayed). But if you call "print" with more than one argument (at "2"), the call to "print" turns into a varargs call with just the address of the reference (myArray ) being stored.
Also, when you try myArray2, it ALWAYS prints just the reference address because "int[]" is not a subtype of Object, etc...
was wondering if there was a rule of thumb of some kind..? ;-) thnx!
- 05-10-2010, 05:05 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
Nope.
If you pass a single (valid type) array in to a varargs, then the array is treated as the vararg (ie each element is an argument).
If you pass multiple objects into the varargs (and you normally wouldn't use Object, which is why you're seeing what you perceive to be wierdness) then each Object is treated as an entry in the array.
So print(myArray1, 9, 2) is essentially creating an Object[] with the first element as myArray1, the second as Integer(9) and the third as Integer(2). So the print uses toString on these, and for an array that is the default toString (ie hashcode).
Similar Threads
-
the strange and weird world of Varargs...
By vlad in forum Advanced JavaReplies: 2Last Post: 05-09-2010, 12:29 AM -
VarArgs Example
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:38 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks