/*Varags is nothing but "variable length argument in a method" i.e. we can define an unspecified number of parameters in a method.
To use varags we used three dot(...) known as "ELIPSIS" as mention in below example.*/
//VaragsClass.java
public class VaragsClass
{
void SumOfArray(int... number) /* (...) is known as elipsis, number is variable name, int is variable type*/
{
int sum=0;
for(int num:number)
{
sum = sum+num;
}
System.out.println("Summation = "+sum);
}
}
//MainClass.java
public class MainClass
{
public static void main(String args[])
{
VaragsClass vrgs = new VaragsClass();
vrgs.SumOfArray(5,10,15,20,25,30); //Unspesified number of parameter
}
}
Output =>
Summation = 105
To use varags we used three dot(...) known as "ELIPSIS" as mention in below example.*/
//VaragsClass.java
public class VaragsClass
{
void SumOfArray(int... number) /* (...) is known as elipsis, number is variable name, int is variable type*/
{
int sum=0;
for(int num:number)
{
sum = sum+num;
}
System.out.println("Summation = "+sum);
}
}
//MainClass.java
public class MainClass
{
public static void main(String args[])
{
VaragsClass vrgs = new VaragsClass();
vrgs.SumOfArray(5,10,15,20,25,30); //Unspesified number of parameter
}
}
Output =>
Summation = 105
No comments:
Post a Comment