-2

I wrote this code and want to see memory location of 2 objects that i create from one class and make instance of one specific variable.

public class StaticFields {

    int a = 12;
    int b = 235;

    public static void main(String[] args) {
      StaticFields obj = new StaticFields();

      int a = obj.a;

      StaticFields obj2 = new StaticFields();

      int c = obj2.a;

       System.out.println(System.identityHashCode(a));
       System.out.println(System.identityHashCode(c));

    }
}


why the "identityHashCode" of "a" and "c" is the same ?

Thanks.

  • 2
    *FYI:* The value returned by `identityHashCode()` has nothing to do with *memory address*. In general, you cannot get the memory address of an object in Java. – Andreas Oct 09 '19 at 11:36
  • 2
    `a` and `c` are both **primitive** `int` values, so when you call `identityHashCode()` which expects an `Object`, the compiler will **autobox** the values to `Integer` objects, and values in range -128 to 127 are cached for performance, so you get the same `Integer` object every time you call the method with value `12`. – Andreas Oct 09 '19 at 11:39
  • [here](https://stackoverflow.com/a/46094854/896249) is another SO-thread on that matter. Including a "workaround" on how to extrapolate the heap-address from an object (the one you see when you use the `toString()` method of a class that does not override it) – GameDroids Oct 09 '19 at 11:45
  • @Andreas thank you for the clarification, I removed my comment. – GameDroids Oct 09 '19 at 11:48
  • @GameDroids `identityHashCode()` *is* the value you see when you don't override `toString()`, and it is *not* a memory address. – Andreas Oct 09 '19 at 11:48
  • @GameDroids The hash code that the method `Object.hashCode` returns may or may not be derived from the memory address, but it still is not the address itself. That also means, that even `Object.hashCode` could return the same hash value for distinct objects. – Seelenvirtuose Oct 09 '19 at 11:49
  • 2
    [How does the default hashCode() work?](https://srvaroa.github.io/jvm/java/openjdk/biased-locking/2017/01/30/hashCode.html) – Erwin Bolwidt Oct 09 '19 at 11:54
  • sorry for being unclear about that. No it is not the memory address. But the `toString()` method uses the objects hashcode, which is used by the JVM to quickly fetch the object from a specified location (and IMHO this is the closes you can and need to get to an address). – GameDroids Oct 09 '19 at 12:05
  • The `hashCode` of an object is not its memory address. – Raedwald Oct 09 '19 at 12:34

1 Answers1

-2

Both the integers carry the same value, 12.

Since Integers are cached (for values up to 127 from -128), the hash code value of both objects returned are same.

This is not true for b since its value is greater than 127.

  • The *value* of an `int` has nothing to do with `identityHashCode()` of an `Object` (parameter type of the method). – Andreas Oct 09 '19 at 11:40
  • Try it with `obj.b` and `obj2.b`. They have also "the same value", but ... – Seelenvirtuose Oct 09 '19 at 11:41
  • @Seelenvirtuose - hashcode value of obj.b and obj2.b is not same. – TheSemicolon Oct 09 '19 at 11:45
  • Thanks for answers . now , my problem is int c and int a has the same memory location ? i mean are they point to one location of memory ? or they have different objects in memory cause they are from separate instance ? – Aryan Gates Oct 10 '19 at 15:42