0

I am new to java, As per my understanding String = "ABC" will be stored in string pool and String s=new String("ABC") will create a new memory to store the value. if my understanding is correct how to prove this without using == or equals() method? Can we prove this using hashcode ? I generated the hashcode value for both, it returns the same value... why is that...

Ramakrishna
  • 386
  • 5
  • 24
  • because even though the object in memory is not the same, the value is, and equals will return true. if equals returns true, the hashcode MUST return the same value (unless you are planning to write code that 'll take some additional hours of debugging) – Stultuske Apr 03 '15 at 10:56
  • is there any way to prove this without using equals? – Ramakrishna Apr 03 '15 at 10:58
  • to prove what, exactly? that the two Objects are equal? you can use the compareTo method – Stultuske Apr 03 '15 at 10:59
  • can you guide me with hashcode – Ramakrishna Apr 03 '15 at 11:00
  • Why don't you want to prove it with the `==` operator? It's the right one. – RealSkeptic Apr 03 '15 at 11:08
  • @Ramakrishna you may want to check this: http://stackoverflow.com/questions/27581/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java – Stultuske Apr 03 '15 at 11:09
  • can you post the code in your project where you find this scenario – vikeng21 Apr 03 '15 at 11:10
  • hashCode returns the same value because constructor new String(String original) actually sets hashcode of new instance equal to hashcode of original. – mkrakhin Apr 03 '15 at 11:15
  • Have a look at [this question][1] which may help. [1]: http://stackoverflow.com/questions/909843/how-to-get-the-unique-id-of-an-object-which-overrides-hashcode – redge Apr 03 '15 at 11:15

2 Answers2

0

... how to prove this without using == or equals() method?

The best way1 to prove it in Java code is to use ==.

Certainly you can't prove it using hashcode on the strings because they will have the same hash code. To understand why that is, read the javadoc for String.hashCode(). It explains how the hashcode for a string is calculated.

1 - You could prove it by comparing the values returned by System.identityHashCode(Object). However, that's a round-about approach, and the proof relies on knowledge of what the identity hashcode actually means.


I generated the hashcode value for both, it returns the same value... why is that...

Read the javadoc ... then you will understand.

Stephen C
  • 632,615
  • 86
  • 730
  • 1,096
-1

You cannot prove this using hashcode, because if two strings value are equals it means that they have the same value of hashcode.

You can prove it by comparing their references by using this operator ==.

Take a look a this What's the difference between ".equals" and "=="? to understand the exact difference between the equal method and the == operator.

Community
  • 1
  • 1
Anarki
  • 353
  • 4
  • 19
  • Two strings with same values have the same hashcode, and this operator `==` compare the reference of the strings. Why this explanation is incorrect ? – Anarki Apr 03 '15 at 13:56