0

so in the code below the behavior is as expected that a == b would return false since I used the keyword new. However when I print the memory addresses I get the same value. I think they should have separate memory addresses, also if they are the same why would == return false. The code below with j k l is the same concept.

    String a = new String("test");
    String b = new String("test");

    System.out.println(a.equals(b));
    System.out.println(a == b);

    System.out.println(Integer.toHexString(a.hashCode()));
    System.out.println(Integer.toHexString(b.hashCode()));

    String j = "this";
    String k = new String("this");
    String l = "this";

    System.out.println(j.equals(l));
    System.out.println(j == l);

    System.out.println(Integer.toHexString(j.hashCode()));
    System.out.println(Integer.toHexString(l.hashCode()));
2
  • See tutorialspoint.com/java/java_string_hashcode.htm Commented Nov 24, 2015 at 19:06
  • String are managed in a special way in Java. The JVM tries to deduplicate them, and keep them in a shared memory. You can give it hints for optimization purposes. Commented Nov 24, 2015 at 19:10

1 Answer 1

6

hashCode is not expected to print the memory address (although the default implementation may). The key here, is that String has overriden the equals() method, and must therefore override the hashCode() method to be consistent with it. That is, any two objects that are equal according to equals() must return the same hashCode. This requirement is documented in the JavaDoc for Object: http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals%28java.lang.Object%29

If you are trying to simply prove that the two references a and b have the same memory address, then that is essentially what Java reveals with ==. The == operator will return true if-and-only-if the two references point to the exact same underlying object. (You can think of this as "having the same memory address"). On the other hand, .equals() is a method that should return true if the objects are semantically the same. (For example two different string objects with the same characters are semantically the same.) Note that equals is a method - it is not special - and the developer is free to introduce bugs into it.

If you want to experiment with the language a bit more to tease out what == really means, try experimenting with a mutable type (like StringBuffer). You will see that if you have two StringBuffer references a and b that are ==, then modifying a will also modify the contents of b. On the other hand, if a and b are not ==, but equals() returns true, modifying one will leave the other intact.

Sign up to request clarification or add additional context in comments.

4 Comments

what method will print the memory address like I want, or how can I override hash to get the behavior I want?
Since Java is platform-independent and running on a virtual machine, the language really abstracts most of that away so that you have no way to get to that. (But hopefully no real reason to need it, either.) Why do you want to know the memory address?
For no practical purpose just trying to show that in the above code they have different memory locations by printing their actual address
I updated the answer to get at some of what you are wanting to know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.