Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you're seeing is because you're using <code>String</code>, which has a very special (and <em>nearly</em> unique) behavior: Your two strings are actually one <code>String</code> object, because string literals are automatically <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#intern%28%29" rel="noreferrer"><code>intern</code>'d</a>. The JDK and JVM work together to put string literals into a pool of <code>String</code> instances which are reused, rather than creating separate <code>String</code> instances for the same sequence of characters.</p> <p>Try your experiment with <code>new Object()</code> instead:</p> <pre><code> Object a = new Object(); Object b = new Object(); System.out.println(a.hashCode()); System.out.println(b.hashCode()); System.out.println(System.identityHashCode(a)); System.out.println(System.identityHashCode(b)); </code></pre> <hr> <blockquote> <p>Also what is difference between hashcode() and system.identityhashcode()</p> </blockquote> <p>The <code>hashCode</code> function can be overridden by a class to return something appropriate for that class. <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#identityHashCode%28java.lang.Object%29" rel="noreferrer"><code>System.identityHashCode</code></a> returns the same <code>hashCode</code> that <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#hashCode%28%29" rel="noreferrer"><code>Object#hashCode</code></a> <em>would</em> have returned if the subclass hadn't overridden it.</p> <p>So for <code>Object</code>, you'd get the same return value from each of them. But for any class that overrides <code>hashCode</code> to return something more appropriate for that class (which includes <code>String</code>), you'd get different values.</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. 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