What is Anonymous array?
class Array16
{
public static void main(String[] args)
{
sum(new int[]{10,20,30,40});
}
public static void sum(int[] x)
{
int total=0;
for(int a:x)
total+=a;
System.out.println("Sum = "+total);
}
}
/*
Output:- 100
Explanation:- In the above program, it is an example of anonymous array(A nameless array
is called anonymous array). The main purpose of anonymous array is just for
instant use(1 time use). Here to call sum() method we required an array but
after completing that sum() method call, we are not using that array anymore.
Hence anonymous array is the best choice for this requirement.
*/
{
public static void main(String[] args)
{
sum(new int[]{10,20,30,40});
}
public static void sum(int[] x)
{
int total=0;
for(int a:x)
total+=a;
System.out.println("Sum = "+total);
}
}
/*
Output:- 100
Explanation:- In the above program, it is an example of anonymous array(A nameless array
is called anonymous array). The main purpose of anonymous array is just for
instant use(1 time use). Here to call sum() method we required an array but
after completing that sum() method call, we are not using that array anymore.
Hence anonymous array is the best choice for this requirement.
*/
0 comments: