Why does the default Object.toString() include the hashcode?

前端 未结 3 709
别跟我提以往
别跟我提以往 2020-11-22 09:40

If you execute:

System.out.println(someObj.toString());

you probably see the output like

someObjectClassname@hashcod

3条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 10:12

    From the javadocs:

    As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)

    The hashCode appears in the string representation of the object so that you can distinguish this object from other objects of the same class. This can be useful for debugging.

提交回复
热议问题