What is the meaning of the information that I get by printing an Object in Java? [duplicate]

孤街醉人 提交于 2019-12-20 03:07:22

问题


Lets say i have this code :

Integer[] a= new Integer[5];
System.Out.println(((Object)a).toString());

the output is get is

[Integer@89fbe3

what is the meaning of 89fbe3 ? is this some kind of address ? hash code? is it unique for each object? , and if so- if its a multi-threaded program , is it still unique ?

thanks !


回答1:


It's the memory address of the object which is what the default toString() implemented in the Object class does. It is also the default hashCode().




回答2:


It's the result of System.identityHashCode(Object x);

which is the default implementation of every object's hashCode()...

from the Object javadoc:

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



回答3:


The 89fbe3 is a hex version of the hash code. The [I means an array of ints (I'm surprised you get that with an Integer[], are you sure it wasn't an int[]?)

Some others:

  • [L<typename>;: an array of reference type "typename" (e.g. [Ljava.lang.Integer)
  • [J: an array of longs
  • [B: an array of bytes

etc.




回答4:


It is the identity hash code of the object (you can think of it as the address of the object), along with some type information.

[ = array I = Integer




回答5:


I think that while technically all the answers are correct, the real answer is "NO". This number has no meaning and you can make absolutely no assumptions about it.



来源:https://stackoverflow.com/questions/4696103/what-is-the-meaning-of-the-information-that-i-get-by-printing-an-object-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!