java怎么打印每个对象的内存地址呢?hashcode能代表内存地址的不同吗?
黄舟
黄舟 2017-04-18 10:16:13
0
4
603

java怎么打印每个对象的内存地址呢?hashcode能代表内存地址的不同吗?

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(4)
黄舟
  1. I remember it’s not allowed. What is the reason for you to print the memory address?

  2. Hashcode does not necessarily mean that the memory addresses are different. Different JVMs have different implementations.

伊谢尔伦

Object’s hashCode() returns the memory address by default, but hashCode() can be rewritten, so hashCode() cannot represent different memory addresses

The System.identityHashCode(Object) method can return the memory address of the object, regardless of whether the object’s class overrides the hashCode() method

Peter_Zhu

The toString method in the Java Object class prints the address of the reference variable by default. If it does not print the address, then toString has been overridden in the subclass. You can try to rewrite toString

PHPzhong

Read the JDK API carefully.
The general protocol of hashCode is:

  • During the execution of a Java application, the hashCode method must consistently return the same integer when called multiple times on the same object, provided that the information used to equals the objects is not modified. This integer does not need to be consistent from one execution of an application to another execution of the same application.

  • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.

  • If two objects are not equal according to the equals(java.lang.Object) method, then calling the hashCode method on either object does not necessarily produce different integer results. However, programmers should be aware that generating different integer results for unequal objects can improve hash table performance.
    In fact, the hashCode method defined by the Object class does return different integers for different objects. (This is typically accomplished by converting the object's internal address to an integer, but the JavaTM programming language does not require this implementation technique.)

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!