Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>How do Object.hashCode() and System.identityHashCode() work at the back end? </p> </blockquote> <p>Assuming that it hasn't been overridden, the <code>Object.hashCode()</code> method simply calls <code>System.identityHashCode(this)</code>.</p> <p>The exact behavior of <code>System.identityHashCode(Object)</code> depends on the JVM implementation. (The actual implementation on recent Hotspot JVMs is rather clever, but I digress.)</p> <blockquote> <p>Does <code>identityHashCode()</code> return the reference of the object? </p> </blockquote> <p>No. It returns an <code>int</code>, and an <code>int</code> cannot hold a reference. (Duh!)</p> <p>That integer returned by <code>identityHashCode</code> may be related to the (a) machine address for the object, or it may not be<sup>1</sup>. The value returned by <code>identityHashCode()</code> <em>is guaranteed not to change</em> for the lifetime of the object. This means that of the GC relocates an object (after an <code>identityHashCode()</code> call) then it cannot use the new object address as the identity hashcode.</p> <blockquote> <p>Does hashCode() depend on the <code>?</code> of the object <code>? ==</code> operator how to work in back end.</p> </blockquote> <p>This doesn't make sense. There is no <code>? ==</code> or <code>?==</code> operator in Java.</p> <blockquote> <p>What is the difference between hashCode() and identityHashCode()?</p> </blockquote> <p>This is partly explained above. Other differences include:</p> <ul> <li><p>The <code>hashcode()</code> method is a non-final instance method, and should be overridden in any class where the <code>equals(Object)</code> is overridden. By contrast, <code>identityHashCode(Object)</code> is a <code>static</code> method and therefore cannot be overridden.</p></li> <li><p>The <code>identityHashCode(Object)</code> method gives you a identifier for an object which can (in theory) be used for other things than hashing and hash tables. (Unfortunately, it is not a <em>unique</em> identifier, but it <em>is</em> guaranteed to never change for the lifetime of the object.)</p></li> </ul> <hr> <p><sup>1 - For current generation JVMs, it is not related to the memory address at all. See @bestsss's answer.</sup></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload