If I attempt to overload the method flexiPrint()
in a class Varargdemo
then it generates a compile time error. The compiler treats the following signatures the same:
public static void flexiPrint(Object... data){}
public static void flexiPrint(Object[] data){}
Can someone explain to me why they are treated the same? I haven't been able to find the answer.
They are the same "under the hood". varargs (the
...
) passes an array as a parameter:You can find it in the documentation here .
Variable Length Arguments, like
Object...
are syntactic sugar. When used, for example:Then "apple", "peach", "plum" are actually turned into: `Object[]{"apple", "peach", "plum"}.
Object...
is nothing but it is an array, that means same as definingObject[]
...
(three dots) represents varargs in java.We usually see this signature in main method like
main(String... args)
So, having more than one method with same signature is not allowed in a class (compile time error). That is why you are seeing compile time error.