Variable Arguments (Varargs) Method (#19098)

if we need a method in which  number of parameters varies then use this type.
This commit is contained in:
vikash vaibhav
2018-10-16 08:59:00 +05:30
committed by Quincy Larson
parent 4f12490bcd
commit 290e16e80d

View File

@ -48,6 +48,28 @@ public class Car {
}
}
```
If we not sure with the distinct number of parameters passed in a method then use 'Variable Arguments (Varargs) Method'
Initialize parameter with three dots and will have same dataType.
Passed arguments will store in an array and also accessible as array
```java
public class Main
{
public static void main(String[] args) {
Main p=new Main();
System.out.println(p.sum(5,5,5,5,5,5,5,5,5)); //can pass any number of argument.
}
int sum(int ...a) //decleration of variable argument method.
{int sum=0;
for(int i=0;i<a.length;i++)
sum=sum+a[i]; //getting the passed arguments in form of array
return sum;}
}
```
As with any other language, methods (or functions, if you are here from JS world) are used often for their modularity and reusability.