I am new to generics and I am not sure if the answer to my question is opinion based
or has a genuine reason. In the following code what was need to case a key
If what you want to do with a class requires you jump through hoops, you do. With generics I have had trouble getting it to make sense all the time. sometimes it turns out easier than you would think, other times you try everything you can think of. keep at it.
Looking at the code for Entry, key is "final". My take is that this code is creating a non-final variable from the final variable so that constraints of a final variable does not prevent the subsequent code from executing. Other than that, I also would assume as you have said both the "if" conditions should be the same.
You've missed the last part of where key
is checked for equality with k
, so removing assignment (k = e.key)
will break the last check -
if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k))))
This is an extreme optimization measure that is probably not necessary for general purpose programming practice. Here is a discussion that could answer your question. Below statement is copied from that post:
It's a coding style made popular by Doug Lea. It's an extreme optimization that probably isn't necessary; you can expect the JIT to make the same optimizations. (you can try to check the machine code yourself!) Nevertheless, copying to locals produces the smallest bytecode, and for low-level code it's nice to write code that's a little closer to the machine.