• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

question concering hashCode(), equals() and operator ==

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Looking at the code below and the result after running the programme.


Here is the result
hash code for str1: -1704812257
hash code for str2: -1704812257

hash code for MyClass obj1: 24216257
hash code for MyClass obj2: 20929799

str1.equals(str2): true
str1 == str2 : false

obj1.equals(obj2): false
obj1 == obj2 : false

Class for str1: class java.lang.String
Class for obj1: class MyClass

Text representation of str1: WhoAmI
Text representation of obj1: MyClass@17182c1
array1 == array2 : false
array1[0] == array2[0] : true
array1[1] == array2[1] : true
array1[2] == array2[2] : true

hash code for MyClass obj3: 16032330
obj1 == obj3 : false



Here are my questions
1. why hash codes of str1 object and str2 object are the same?
2. why hash codes of obj1 and that of obj2 are different?
3. what does equals() method do?
4. what does operator == do?
5. why str1.equals(str2) return true? but str1==str2 returns false?
6. why array1 == array2 return false
7. and why obj1 == obj3 return false even obj3 is cloned from obj1.

Thanks

Sura
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


1. why hash codes of str1 object and str2 object are the same?


Because the value of them is same, and they are strings. Thus, the hash value of them will also be same. If they are not of string type (or Wrapper class types), the hash code will not be the same.


2. why hash codes of obj1 and that of obj2 are different?


Because they are 2 different objects. Hash code will only be the same for the same object.


3. what does equals() method do?


It compares the values of the 2 objects to see whether they are equal.


4. what does operator == do?


It compares the memory location of the 2 objects to see whether they are pointing to the same memory address.


5. why str1.equals(str2) return true? but str1==str2 returns false?


Because the 2 strings share the same value, but they are 2 different objects (i.e. with different memory locations).


6. why array1 == array2 return false


== operation will only return true, if and only if the 2 objects are of the same memory location.

For example:
Object a = new Object();
Object b = a;

Then, a == b returns true.


7. and why obj1 == obj3 return false even obj3 is cloned from obj1.


Cloning means creating a new object based on another object. As said, they are 2 different objects that share the same value.

Hope this help.

Nick
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nick,

Hash code will only be the same for the same object.

According to the Java api, it's not a requirement for two different objects to have distinct hash code.


The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.



  • Joyce
    [ October 13, 2004: Message edited by: Joyce Lee ]
     
    There are 29 Knuts in one Sickle, and 17 Sickles make up a Galleon. 42 tiny ads in a knut:
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
    reply
      Bookmark Topic Watch Topic
    • New Topic