Results 1 to 7 of 7
  1. #1
    vlad is offline Member
    Join Date
    May 2010
    Location
    Colorado
    Posts
    6
    Rep Power
    0

    Default 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);
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

  4. #4
    vlad is offline Member
    Join Date
    May 2010
    Location
    Colorado
    Posts
    6
    Rep Power
    0

    Default

    oops... sorry.. was not aware of that :-)

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default

    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?

  6. #6
    vlad is offline Member
    Join Date
    May 2010
    Location
    Colorado
    Posts
    6
    Rep Power
    0

    Default 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!

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default

    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

  1. the strange and weird world of Varargs...
    By vlad in forum Advanced Java
    Replies: 2
    Last Post: 05-09-2010, 12:29 AM
  2. VarArgs Example
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •