0

I've been wondering what the value you get means when you print an array in java.

float[] array = new float[] {1f,1f,1f,1f};
System.out.println(array);

The output I receive is [F@7fbe847c
I assume the F means float (correct me if I am wrong)

I notice that when I use a string array, some things change

String[] array = new String[] {"a","a","a","a"}
System.out.println(array);

the output now is [Ljava.lang.String;@7fbe847c
I assume the Ljava.lang.String; means it is a string array (correct me if I am wrong).
Either way, the @7fbe847c stays the same.

My question isn't how to print an array (I already know to use Arrays.toString()), my question is what does the value mean and what is it normally used for?

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
irishpatrick
  • 137
  • 1
  • 13

2 Answers2

2

as mentioned above, just printing will invoke the toString method of the object. As this method has not be overriden, the toString method of Object gets invoked. The exact output is specified in the javadocs http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString()

which says

 getClass().getName() + '@' + Integer.toHexString(hashCode())

i.e. it is the class name + "@" + the HexString of the objects hashCode() method

Ueli Hofstetter
  • 2,409
  • 4
  • 29
  • 52
1

Every object has a toString() method, and the default method is to display the object's class name representation, then "@" followed by its hashcode.The general contract of hashCode is, Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified So what you're seeing is the default toString() representation of float and string array.

kk1992
  • 153
  • 1
  • 3