Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The value returned by <code>hashCode()</code> is by no means guaranteed to be the memory address of the object. I'm not sure of the implementation in the <code>Object</code> class, but keep in mind most classes will override <code>hashCode()</code> such that two instances that are semantically equivalent (but are not the same instance) will hash to the same value. This is especially important if the classes may be used within another data structure, such as Set, that relies on <code>hashCode</code> being consistent with <code>equals</code>.</p> <p>There is no <code>hashCode()</code> that uniquely identifies an instance of an object no matter what. If you want a hashcode based on the underlying pointer (e.g. in Sun's implementation), use <code>System.identityHashCode()</code> - this will delegate to the default <code>hashCode</code> method regardless of whether it has been overridden.</p> <p>Nevertheless, even <code>System.identityHashCode()</code> can return the same hash for multiple objects. See the comments for an explanation, but here is an example program that continuously generates objects until it finds two with the same <code>System.identityHashCode()</code>. When I run it, it quickly finds two <code>System.identityHashCode()</code>s that match, on average after adding about 86,000 Long wrapper objects (and Integer wrappers for the key) to a map.</p> <pre><code>public static void main(String[] args) { Map&lt;Integer,Long&gt; map = new HashMap&lt;&gt;(); Random generator = new Random(); Collection&lt;Integer&gt; counts = new LinkedList&lt;&gt;(); Long object = generator.nextLong(); // We use the identityHashCode as the key into the map // This makes it easier to check if any other objects // have the same key. int hash = System.identityHashCode(object); while (!map.containsKey(hash)) { map.put(hash, object); object = generator.nextLong(); hash = System.identityHashCode(object); } System.out.println("Identical maps for size: " + map.size()); System.out.println("First object value: " + object); System.out.println("Second object value: " + map.get(hash)); System.out.println("First object identityHash: " + System.identityHashCode(object)); System.out.println("Second object identityHash: " + System.identityHashCode(map.get(hash))); } </code></pre> <p>Example output:</p> <pre><code>Identical maps for size: 105822 First object value: 7446391633043190962 Second object value: -8143651927768852586 First object identityHash: 2134400190 Second object identityHash: 2134400190 </code></pre>
    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.
 

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