This was introduced in java 1.5
It means variable arguments
Background
Before 1.5, if we need to pass different number of arguments to a method we need to use method overloading or pass it in an Array.
public class
TestVarargs
{
static
void
funtion(
int
...numbers)
{
System.out.println(
"Number of arguments: "
+
numbers.length);
for
(
int
i: numbers)
System.out.print(i +
" "
);
}
public
static
void
main(String args[])
{
funtion(
100
);
funtion(
11
,
22
,
55
);
funtion();
}
}
Varargs
(1) Allows us to pass arbitery no of arhuments automatically.
(2) There can be only one varargs in a method and it should be the final argument.
(3) Very good with concrete data types but be careful while using with Generics.
(4) 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.
Hope this helps :)
No comments:
Post a Comment