This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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
posted
0
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
posted
0
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
posted
0
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.