File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question concering hashCode(), equals() and operator == Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question concering hashCode(), equals() and operator ==" Watch "question concering hashCode(), equals() and operator ==" New topic
Author

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

sura watthana
Ranch Hand

Joined: Sep 13, 2004
Posts: 77
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
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982

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


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
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 ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: question concering hashCode(), equals() and operator ==