String args Types in Java
Syntax :
(String args[])
or
(String []args)
or
(String ....args) // new compiler
Example :
class ArrayArgument
{
static int sum(int arr[])
{
int s=0;
for(int i=0;i<arr.length;i++)
{
s+=arr[i];
}
return s;
}
public static void main(String args[])
{
int n1[]={5,6,7};
int ans1=sum(n1);
System.out.print("sum is "+ans1);
}
}
output :
Example :
(String....args);
class VarArgs
{
static int sum(int...arr)
{
int s=0;
for(int i=0;i<arr.length;i++)
{
s+=arr[i];
}
return s;
}
public static void main(String args[])
{
int ans1=sum(5,6,7); // here is not compulasary create an array
because (string...args)is create array automatically
System.out.println("sum is "+ans1);
}
}
output :