1
public class Demo {
    public static void main(int ...i) {
        System.out.println(i);
    }
}

class Demo1 {
    public static void main(String ...s) {
        Demo.main(1);
    }    
}

answer is [I@659e0bfd

beresfordt
  • 5,088
  • 10
  • 35
  • 43
Abhi
  • 23
  • 3

5 Answers5

5

From the documentation of varargs which denotes a variadic function:

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.

As understood, i is an array of integers in Demo. What you probably want is:

public class Demo {
    public static void main(int ...i) {
        System.out.println(i[0]);
    }
}

Make sure to test the array's length.

Currently you're printing the class: [I@659e0bfd This denotes an array of integers (notice the bracket [ which means array and I which means integer).

Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88
  • thanks bro but if changes int to string .. it is not going to work .. public try class Demo { public static void main(String ...i){ System.out.println(i); } } class Demo1{ public static void main(String ...s){ Demo.main("a"); } } – Abhi Aug 09 '15 at 08:35
2

i is an array, that's what the ...i notation means: "All the arguments from this point onward, as an array." That's why when you output it, you get that default string from the array's toString method.

To output the contents of the array, either loop through it or index into it:

for (int n : i) {
    System.out.println(n);
}

If you put that in Demo.main, you'd see it output 1 (since that's the only entry in the array).

Here's another example of ... notation, which hopefully makes it a bit clearer:

static void foo(String first, String second, String ...more) {
    System.out.println("first: " + first);
    System.out.println("second: " + second);
    for (String s : more) {
        System.out.println("and: " + s);
    }
}

If you call that:

foo("one", "two", "three", "four", "five");

Note how I'm calling it with five arguments, but only three are declared. The third one, more, with the ..., gets all of the arguments from that point onward. So the call above gives us

first: one
second: two
and: three
and: four
and: five
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

int ...i means varargs. So though you pass 1 as a int (not as an array) you have to access the varargs similar to array e.g. i[0] -> 1 Since you are printing just the varargs it will print the hashcode of the object

0

The toString Method on array objects is not implemented in a way you expect it. You have to use Arrays.toString() to print the array content.

System.out.println(Arrays.tostring(t));

The reason you get an array is becaus of the ellipses. These are var args in java. That means, all the arguments for this parameter get wrapped in a array. Something like this:

 Demo.main(new int[] {1});//compiler generated code
Community
  • 1
  • 1
morpheus05
  • 4,772
  • 2
  • 32
  • 47
0
i

is an integer array. By printing i you are printing the reference variable not the element of the array.

As everything is represented via objects in java except primitive datatypes printing i gives proxyclassname@hexadecimalcode of class calculated by the JVM.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Gaur93
  • 685
  • 7
  • 19