1

In order to test a program I wrote these statements:

public class asdf{
  public static void main(){
    int arr[] = new int[5];
    System.out.println(arr);
  }
}

The output I got was:

[I@1df6ed6

Is this a garbage value or something different?

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Rayyan Merchant
  • 157
  • 3
  • 11
  • I'm no java expert, but it occurs to me you might have to convert the array to a string before you print it. –  Dec 05 '15 at 03:52
  • 1
    Possible duplicate of [What happens when printing an object in java](http://stackoverflow.com/questions/20735132/what-happens-when-printing-an-object-in-java) – ricky3350 Dec 05 '15 at 03:56

2 Answers2

2

What is being printed is the memory address of the object. In order for the object to print a human-readable string it has to have toString() implemented in such a way that it is human-readable. Memory addresses will change each time you execute a program, and are generally represented in a way that is not particularly useful (unless you're doing deep magic with the operating system).

One way to get this is to use the java.util.Arrays utility to convert each element of the array to a String.

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
  • 3
    Not that it really matters, but it's really the [identity hash code](http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#identityHashCode(java.lang.Object)), which is slightly different. – ricky3350 Dec 05 '15 at 03:54
1

The "garbage value" is actually the address of the array (where it is stored in memory).

If you want to print the contents, you need to first import java.util.Arrays; and then

System.out.println(Arrays.toString(myArray);
pushkin
  • 9,575
  • 15
  • 51
  • 95
  • I actually wanted to know what is [I@1df6ed6 as I have never encountered this before. So does this value have a name. – Rayyan Merchant Dec 05 '15 at 03:51
  • It is simply the address of the array. It points to the array in memory. In Java, this will probably have little use for you. – pushkin Dec 05 '15 at 03:53
  • Look at [this](http://stackoverflow.com/questions/14794930/why-does-printing-a-java-array-show-a-memory-location). – pushkin Dec 05 '15 at 03:54
  • Actually as ricky pointed out below, it's not technically the address, but the hash. – pushkin Dec 05 '15 at 03:55