SCJP 1.6, SCWCD 5.0, SCBCD 5.0 [loading..]
Himalay Majumdar wrote:
But if I do the same treatment for Cat without overriding hashcode and equals, I get the result i.e
// 2
I can get the value "Cat key" in the output instead of null. So what does the explanation in K&B means.
Also, if I put the following code in the above program, it gives me null pointer while putting element in the map.
Can someone clarify me on this above behavior.
All code in my posts, unless a source is explicitly mentioned, is my own.
Ruben Soto wrote:
Why can you retrieve the cat? Because you are using a reference to the same object in lines // 1 and // 2.
Because of the contracts of hashCode() and equals() (the results must be consistent when called different times on the same object,) that means that you will get the same hashCode() and equals() results in // 1 and // 2
You say that you are getting a NullPointerException at line // 3. Would you mind posting the full code where this happens, and the error output that you get?
SCJP 1.6, SCWCD 5.0, SCBCD 5.0 [loading..]
All code in my posts, unless a source is explicitly mentioned, is my own.
SCJP 1.6, SCWCD 5.0, SCBCD 5.0 [loading..]
All code in my posts, unless a source is explicitly mentioned, is my own.
SCJP 1.6, SCWCD 5.0, SCBCD 5.0 [loading..]
All code in my posts, unless a source is explicitly mentioned, is my own.
SCJP 1.6, SCWCD 5.0, SCBCD 5.0 [loading..]
Consider Paul's rocket mass heater. |