int[] ar;
ar = new int[5];
System.out.println(ar);
System.out.println(new int[5]);
—-output —–
[I@15db9742
[I@6d06d69c
Why not these addresses aren’t same?
edit– It’s not about array printing. I want to ask about difference between these addresses.
>Solution :
Because they are different five element int arrays consisting of 0(s). They could contain any five elements.
new int[5]
is equivalent to
new int[] {0, 0, 0, 0, 0};
But they are not the same object (or the same array as any other new int[]).
Your code is equivalent to
int[] ar = new int[5];
System.out.println(ar);
int[] br = new int[5];
System.out.println(br);
ar and br are distinct. We can run that interactively in jshell to see
jshell> int[] ar = new int[5];
ar ==> int[5] { 0, 0, 0, 0, 0 }
jshell> int[] br = new int[5];
br ==> int[5] { 0, 0, 0, 0, 0 }
jshell> System.out.println(ar);
[I@52cc8049
jshell> System.out.println(br);
[I@27973e9b