Is array size is mandatory for assigning one array to another array?
class Array22
{
public static void main(String[] args)
{
int[] a={5,4,3,2,1};
int[] b={10,20};
b=a;
for(int x:b)
System.out.println(x);
}
}
/*
Output:- 5 4 3 2 1
Explanation:- Whenever we are assigning one array to another array, internal elements
won't be copied just reference variable will be reassigned and hence
sizes are not required to match but type should be matched.
*/
{
public static void main(String[] args)
{
int[] a={5,4,3,2,1};
int[] b={10,20};
b=a;
for(int x:b)
System.out.println(x);
}
}
/*
Output:- 5 4 3 2 1
Explanation:- Whenever we are assigning one array to another array, internal elements
won't be copied just reference variable will be reassigned and hence
sizes are not required to match but type should be matched.
*/
0 comments: