-4

I was learning about the Singleton design pattern through a tutorial. Basically from what I understand it ensures that only one instance of a class is ever created, and any interaction with that class is done through that one instance in the heap. with that said, in the SingletonPatternDemo.java I wrote:

SingleObject object1 = SingleObject.getInstance();

SingleObject object2 = SingleObject.getInstance();

from my understanding, both object1 and object2 reference the same object, thus they should point to the same location in memory.

However when I write:

System.out.println("object1 HashCode: " + System.identityHashCode(System.identityHashCode(object1)));

System.out.println("object2 HashCode: " + System.identityHashCode(System.identityHashCode(object2))); 

after I run it I get:

object1 HashCode: 865113938
object2 HashCode: 1442407170

since these two objects reference the same instance shouldn't they return the same hashcode? or am I missing something?

Singleton class

Demo class

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
  • you're printing hash code of temp variable with hashcode – Iłya Bursov Aug 11 '18 at 16:22
  • Why call System.identityHashCode() twice? – moilejter Aug 11 '18 at 16:22
  • 1
    The traditional way to check if you have references to the same thing is to ask `object1 == object2` ... – moilejter Aug 11 '18 at 16:23
  • Possible duplicate of [How do hashCode() and identityHashCode() work at the back end?](https://stackoverflow.com/questions/4930781/how-do-hashcode-and-identityhashcode-work-at-the-back-end) – Nicholas K Aug 11 '18 at 16:24
  • @moilejter this is just the way I saw was being done in a tutorial – juned alam Aug 11 '18 at 16:25
  • Welcome to SO. Please don't post the code as an image. Use `System.identityHashCode(object1)` and `System.identityHashCode(object2)` to get the hash code of the object. Or you may simply use `object1.hashCode()` and `object2.hashCode()`. – Abdul Wadood Aug 11 '18 at 16:37
  • This could be a fine question if you would not be using images in it. – GhostCat Aug 11 '18 at 17:58

1 Answers1

3

System.identityHashCode(System.identityHashCode(object1))

The inner System.identityHashCode(object1) will return an int that is the hash code of object1.

The outer call takes an Object - the int is boxed to an Integer. As it's outside of the cached range, the Integer object is created. It is this Integer for which the hash code is displayed.

System.identityHashCode(System.identityHashCode(object2))

Here, the inner call will return an int - the same one as for object1. But, as above, when you call System.identityHashCode the int is boxed to an Integer - a different instance to the above.


By way of illustration:

public static void main(final String[] args) {
    final String hello = "Hello World";
    System.out.println(System.identityHashCode(hello));
    System.out.println(System.identityHashCode(hello));

    System.out.println(System.identityHashCode(System.identityHashCode(hello)));
    System.out.println(System.identityHashCode(System.identityHashCode(hello)));
}

Output:

//first two are the same
899644639
899644639
//second two differ
530737374
1332668132

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166