This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes equals(Object) of String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "equals(Object) of String" Watch "equals(Object) of String" New topic
Author

equals(Object) of String

Yu Chen
Greenhorn

Joined: Dec 03, 2001
Posts: 28
Here is equals method in a class Foo, will it run faster if I use the hashcode of String at line return obj.equals(this.id) ?

Public class Foo {
private String id = "";

public boolean equals(Object obj) {
if(obj == this) return true;
if(obj == null || !(obj instanceof(Foo))) {
return obj.equals(this.id);
}
}
}
[ March 07, 2005: Message edited by: Yu Chen ]

SCJP2<br />SCWCD
Yu Chen
Greenhorn

Joined: Dec 03, 2001
Posts: 28
So the answer is "NO". Here is a test program, any comments welcome.

/*
* TestString.java
* test | int | str eauals/hashcode ==
* 1 | 100,000 | 10/10 , 9/8
* 2 | 1,000,000 | 34/33
* 3 | 10,000,000 | 285/277, 265/260, 264/303, 263/259
*
* Created on March 7, 2005, 9:38 AM
*/

package examples.colorpicker;

public class TestString {
private String id = "This is a test.";

/** Creates a new instance of TestString */
public TestString() {
}

public boolean equals(Object obj) {
if(obj == this) return true;
if(obj == null || !(obj instanceof TestString)) {
return false;
}
TestString ts = (TestString) obj;
//return ts.id.equals(this.id); //compares str length, chars, or nullity.
return ts.id.hashCode() == this.id.hashCode();
}

public static void main(String[] args) {
TestString ts = new TestString();
String sample = "This is a test.";
long start = System.currentTimeMillis();
for(int i = 0; i<10000000; i++) {
ts.equals(sample);
}
long end = System.currentTimeMillis();

System.out.println("Programs runs for " + (end - start));
}
}
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
The line

if(obj == null || !(obj instanceof(Foo)))

can just be

if(!(obj instanceof Foo))

instanceof will return false if obj is null.
Yu Chen
Greenhorn

Joined: Dec 03, 2001
Posts: 28
True, I also tested, it runs a little bit faster w/o the nullity check. Null is not an instance of anything, so all instanceof tests with a null reference produce false.

Thanks!
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
Correct me if I'm wrong, but it is possible that 2 String objects return the same hash code but is not meaningfully equivalent. Hence, it is inappropriate/wrong to replace the equals() with hashCode() in your situation.

Since what you're doing wasn't correct, it won't be a fair test to say that the replacement did not result in a better performance.

Just my thoughts


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: equals(Object) of String