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

前端 未结 3 647
别跟我提以往
别跟我提以往 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:03

    The object hash code is the only standard identifier that might allow you to tell different arbitrary objects apart in Java. It's not necessarily unique, but equal objects normally have the same hash code.

    The default toString() method shows the object class and its hash code so that you can hopefully tell different object instances apart. Since it is also used by default in error messages, this makes quite a bit of sense.

    See the description of the hashCode() method for more information.

提交回复
热议问题