0

Everytime I run this program that I want to display the memory address, it always output same result:

public static void main(String[] args) throws InterruptedException {
    String foo = "a";
    String foo2 = "a";
    System.out.println(Integer.toHexString(foo.hashCode()));
    System.out.println(Integer.toHexString(foo2.hashCode()));

    foo = "a";
    foo2 = "a";
    System.out.println(System.identityHashCode(foo));
    System.out.println(System.identityHashCode(foo2));

    Thread.sleep(1000000);
}

The purpose of this is I am doing some performance test between Streams and Imperative style. So I would like to know if this value is cache, and determines if I should restart my pc or manually run garbage collection via JConsole / JvisualVm.

richersoon
  • 4,682
  • 13
  • 44
  • 74

2 Answers2

-1

String foo2 = "a"; may reuse an instance from the string constant pool if one is available.
this can be proved by

System.out.println(foo==foo2);

== on two reference types is a reference identity comparison. Two objects that are equals are not necessarily==

it output true means foo and foo2 reference the same memory.

Related issues
How do I compare strings in Java?
What is the difference between “text” and new String(“text”)?

Community
  • 1
  • 1
nail fei
  • 2,179
  • 3
  • 16
  • 36
-1

If you want to find out the memory location of object, you can use sun.misc.Unsafe. But i wouldn't use this approach in real life. Just out of the curiosity :)

Here, the explanation of how you can do this: How can I get the memory location of a object in java?

BTW. Object.hashCode() returns the random number. By default it will use Lehmer random number generator.

Community
  • 1
  • 1
i.merkurev
  • 465
  • 2
  • 8
  • Doesn't answer the question that was asked. By default, ['as much as is reasonably practical, the `hashCode` method defined by class `Object` does return distinct integers for distinct objects'](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--). Nothing specified about Lehmer random number generators. – user207421 Feb 09 '17 at 09:08
  • Actually the algorithm of generating identity hashCode can be altered by -XX:hashCode JVM option. There are the following options: -XX:hashCode=0 - global Park-Miller RNG (Lehmer random number generator. default until Java 7); -XX:hashCode=1 - function(obj_address, global_state); -XX:hashCode=2 - constant 1. All objects will have the same hashCode. Just for testing.; -XX:hashCode=3 - incremental counter.; -XX:hashCode=4 - lower 32 bits of the object address in the Heap; -XX:hashCode=5 - thread-local Marsaglia's Xor-shift RNG (default since Java 8); – i.merkurev Feb 09 '17 at 09:29
  • Exactly, so 'by default' what it does isn't specified. – user207421 Feb 09 '17 at 10:28
  • You can edit the answer and put the detail in there about random etc. Please link a reference, it's interesting. – weston Feb 09 '17 at 12:10
  • OK, well that doesn't mention the options. I guess you got it from here: http://stackoverflow.com/a/26049632/360211 or same place as that user did. – weston Feb 12 '17 at 21:41
  • @weston Perhaps, you didn't read the article yet. Right after a chapter **The Actual Identity Hash Generation** you will find these options. – i.merkurev Feb 12 '17 at 21:59
  • I saw it. No mention of the JVM flags at all on that page though. – weston Feb 12 '17 at 22:02