Welcome

First Impression Is Really Important

I have attended a 3 day workshop for Oracle Coherence Product. Altough I am a veteran Eclipse user, we used JDeveloper 11g Technology Preview 3 during our lab sessions. During those lab sessions, I noticed an ugly thing related with JDeveloper. JDeveloper provides some IDE mechanism to generate equals and hashCode methods for your classes. However, it fails to generate hashCode methods correctly when your classes contain primitive fields which need to be included in hashCode generation.

public class Foo {
	private int id;
	public int hashCode() {
		final int PRIME = 37;
		int result = 1;
		return result;
	}
}

Above is the code block I just generated using JDeveloper. Altough I chosed id to include in when generating hashCode, it doesn’t have it. As a result, you have same hashCode value for all your Foo objects. Not good if you are dealing with data structures like Hashtable in your application. In order to generate it correctly, you simply need to convert your primitive type to its Java wrapper equivalent. After this conversion, it looks more appropriate than above;

public int hashCode() {
	final int PRIME = 37;
	int result = 1;
	result = PRIME * result + id == null) ? 0 : id.hashCode(?;
	return result;
}

In summary, I think, it is really important for a new product to handle some fundamental tasks appropriately in order to get acceptance by its users.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.