1

For the code

int []arr = new int[4];
System.out.println(arr);

The output looks something like

[I@54640b25

What exactly is the compiler printing out? The memory address of arr? Unlike C, Java does not seem to equate the array name (in isolation) with the first position of the array.

Maroun
  • 94,125
  • 30
  • 188
  • 241
Stumbler
  • 2,056
  • 7
  • 35
  • 61
  • A good question, but I believe it has already been addressed here. Possible duplicate of [How to use the toString method in Java?](http://stackoverflow.com/questions/3615721/how-to-use-the-tostring-method-in-java) – Paul Richter Mar 19 '14 at 13:58

2 Answers2

6

In Java, each object has toString() method, and arrays are objects. The default is displaying the class name representation, then adding "@" and then the hashcode:

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object

Try to print the following line and you should get the same output:

int[] arr = new int[5]; 
System.out.println(arr.getClass().getName() + "@" + Integer.toHexString(arr.hashCode()));
Maroun
  • 94,125
  • 30
  • 188
  • 241
3

Use the following in order to print the value of array:

Arrays.toString(arr);

Using System.out.println(arr) directory will print use the default toString method which returns:

object.getClass().getName() + "@" + Integer.toHexString(object.hashCode())
Salah
  • 8,567
  • 3
  • 26
  • 43