I tried to print all the elements in an array using both for loop and foreach loop.
In for loop, I got addresses of elements instead of elements themselves. But by using for loop I got the elements themselves. So how this is working even I didn't override toString method also but am getting elements!!
public class ArrayReturn {
public static int[] Multi(int []a)
{
for (int i = 0; i < a.length; i++) {
a[i] = a[i]*2;
}
return a;
}
public static void main(String[] args) {
int ar[] = {2,3,4,5,6,7};
int z[] = Multi(ar);
for (int i = 0; i < z.length; i++) {
System.out.println(z);
}
for (int i : z) {
System.out.println(i);
}
}
}
OUTPUT
[I@5a07e868
[I@5a07e868
[I@5a07e868
[I@5a07e868
[I@5a07e868
[I@5a07e868
4
6
8
10
12
14
I expected either address from both loops or elements. But I got address in for loop and elements in foreach loop.