Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

array object address and reference variable address in java

    int[] ar;
    ar = new int[5];

    System.out.println(ar);
    System.out.println(new int[5]);

—-output —–

[I@15db9742

[I@6d06d69c

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading