Question

I have searched the forum but I am still unable to understand the following differences:

    1) void sum(5,5,5,6,7,8,9,3,5,3,2,3,6){}
    2) void sum(int[] i){}
    3) void sum(int... i){}

And if 3) can be treated as array then what about 1)?

Was it helpful?

Solution

void sum(5,5,5,6,7,8,9,3,5,3,2,3,6){} won't even compile because it is not a valid method declaration

A method parameter list in parenthesis should be a comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses.

2 & 3 are basically the same thing except if you will call the method sum(5,5,5); it won't resolve to method void sum(int[] a ){} because this method is expecting an int array as an calle method argument whereas void sum(int... a){} will work fine as it is expecting variable number of int type arguments in method call.

OTHER TIPS

2 and 3 are basically the same thing. 3 is more elegant because you dont have to create the array before calling the method. Java does that for you.

1 is useful if the number of arguments are never going to change.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top