0
class VarArgs {
    public static void printArray(Object... args) {
        for (Object obj : args)
            System.out.print(obj + " ");
        System.out.println();
    }

    public static void main(String[] args) {
        printArray( new Integer[] { 1, 2, 3 });
    }
}

The console's output is:

   [Ljava.lang.Integer;@1888759 

just want to know what is this output. The supposed castiing was via Object[] and that gives 1,2,3 as output, but when I use Object for casting I get this output

Pshemo
  • 122,468
  • 25
  • 185
  • 269
user2440665
  • 101
  • 5
  • 2
    it's the address of the place in memory where that array is stored. Which is the result of the base implementation of the `toString()` method – Marco Forberg May 31 '13 at 14:02
  • Have a look at this question. [http://stackoverflow.com/questions/5289393/casting-variables-in-java][1] It explains about casting. [1]: http://stackoverflow.com/questions/5289393/casting-variables-in-java – awsome May 31 '13 at 14:20
  • can anyone please explain this ellipse concept used here – user2440665 May 31 '13 at 15:30

4 Answers4

1

You can use

Arrays.toString(new Integer[]{1,2,3}); 

to view the actual contents of the array.

Or cast new Integer[]{1,2,3} to Object[] instead of Object, i.e:

printArray((Object[])new Integer[]{1,2,3});
jontejj
  • 2,800
  • 1
  • 25
  • 27
0

That is not the same as passing an array! That is the same as passing a variable length of parameters, so the method treats the array as one object, not an array of objects, hence the printout.

public static void printArray(Object[] args) {
    for (Object o : args) {
        System.out.println(o);
    }
}

Object[] objects = new Integer[] { 1, 2, 3, 4 };
printArray(objects);

Notice that you need to use the wrapper for int, primitive types are not subclasses of object.

arynaq
  • 6,710
  • 9
  • 44
  • 74
0
  • Explanation of what is happening in your code:

    1. When you are calling the printArray method you are casting your array to an Object so in fact you are passing just one object (not an array).

    2. The foreach loop in the printArray method iterates only once as only one argument has been passed to the printArray method - the Integer[] {1,2,3} array.

    3. Therefore When your code is calling toString, the toString from the Array class is called, not the toString from the Integer class as you might expect.

    4. Finally the result you got: [Ljava.lang.Integer;@1888759 is caused by the lack of the implementation of the toString method in the array classes in Java.

  • To fix the issue in your code:

    Replace:

    printArray((Object)new Integer[]{1,2,3});
    

    with:

    printArray((Object[])new Integer[]{1,2,3});
    
  • To print the content of an array call:

    Arrays.toString(new Integer[]{1,2,3}); 
    
Adam Siemion
  • 15,569
  • 7
  • 58
  • 92
0

As Marco said, this uses the default toString method of an Object, which is its (virtual) memory address.

I would recommend using the Arrays.toString method here

With regards to your use of variable arguments - try disassembling the class file by running

javap -verbose VarArgs

to see what this compiles down into - you should see that your integer array is being passed as the single element of an array.

selig
  • 4,834
  • 1
  • 20
  • 37
  • Sounds like it uses the default toString() of an array as well (it just happens array toString() is the same as Object.toString()) The way you phrase it makes it sound like the cast affects which toString() is called (which it shouldn't since everything in Java is virtual). The cast merely hides the arrayness from the variable method call. –  May 31 '13 at 14:13