aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes equals and == 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 "equals and ==" Watch "equals and ==" New topic
Author

equals and ==

prakash sodhani
Greenhorn

Joined: Nov 04, 2004
Posts: 11
Hi all,

In the following code:

class G {
public static void main (String[] args) {
Long l1 = new Long("1"), l2 = new Long("1");
int h1 = l1.hashCode(), h2 = l2.hashCode();
StringBuffer s1 = new StringBuffer("1");
StringBuffer s2 = new StringBuffer("1");
int h3 = s1.hashCode(), h4 = s2.hashCode();
System.out.print((l1.equals(l2) & (h1==h2)) + ",");
System.out.print(s1.equals(s2) | (h3==h4));
}}

The answer as given is :true/false.

I know first "true" is ok. But for the other part, is it not that h3 and h4 may or may not return the same value and hence the expression

(s1.equals(s2) | (h3==h4));

can't be determined. I mean answer can be false or true because of | operator. But the answer key says it (h3==h4) will always be false.

As per me answer should be "None of the above"(one of the options)

Please let me know if I am wrong.

Thanks
Prakash
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
in this code, s3==s4 will always be false

StringBuffer inherits its hashCode method from Object, which means the memory address of the StringBuffer will be returned. As new StringBuffer objects are created and assigned to s1 and s2, they are different StringBuffers and therefore reside at different memory locations.
Eric Pramono
Ranch Hand

Joined: Jul 09, 2001
Posts: 74
Hi Prakash,

StringBuffer class does not override the equals() method, thus invoking the equals() method on a StringBuffer object will invoke the equals() method in the Object class. Since s1 and s2 are two different StringBuffer objects, the equals() method in the Object class will return false.

- eric
blog
Mathangi Shankar
Ranch Hand

Joined: Nov 01, 2004
Posts: 56
Hi Eric,

I know that StringBuffer does not override equals() method and hence returns false as their instances are different.

But how about the hashcode values of the stringbuffer. Do they reamin same or have different values?

To put it simply h3==h4 returns true or false?

Please clarify...


--------------------------------<br />SCJP1.4
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Hi Mathangi,

Yes, h3 == h4 returns false.

The following info was extracted from the Object - hashCode API.

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)


Joyce
[ November 05, 2004: Message edited by: Joyce Lee ]
Eric Pramono
Ranch Hand

Joined: Jul 09, 2001
Posts: 74
Hi Mathangi,

As Joyce explained, the StringBuffer class also does not override the hashCode() method implementation of the Object class. Hence, when the hashCode() method is invoked on a StringBuffer instance, the hashCode() method in the Object class is invoked instead.

Please refer to your Javadoc, Object.hashCode() for more details on the equals() & hashCode() contract..

- eric
blog
prakash sodhani
Greenhorn

Joined: Nov 04, 2004
Posts: 11
If I remember correctly, in K&B book..it says "If equals methond return false..there is no restrction on hashcode method. Then how, in this case, we can be certain that hashcode will always be different."

Kinda confused....

Thanks for the help
Prakash
Eric Pramono
Ranch Hand

Joined: Jul 09, 2001
Posts: 74
Hi Prakash,

The statement would be correct if we're talking about classes other than the Object class. But, when we're talking about the Object class, the implementation of the hashCode() method in the Object class will return the unique memory address which was allocated to store the object created.

Hence, we can always be certain that when two objects are created differently, they will have different memory address assigned to them, as well as different hashCode() value (if they're using the hashCode() method implementation in the Object class).

- eric
blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: equals and ==