Braindead Long.hashCode?

Did anyone expect Long(-1).hashCode() to return -1? It seems to me that new Long(o).hashCode() == o is an invariant in sun j2sdk1.4. I spent 2 hours looking for the error in my unit test until if found this one:

The two test object instances I used differed only in one property. It was null in one instance and a new Long(0) in the other. The hashcode always ware the same because commons-lang’s HashCodebuilder does sth. like

hashCode += 37  * (fielda == null ? 0 : fielda.hashCode())

Oh my. Never never use a special value like "0" in your unit test.

{ Comments are closed! }