Yes, there is a unfortunately a rule that you may have to commit to memory:
When 2 wrapper instances created through autoboxing will always be == when there primitive values are the same. This rule only applies to the following:
Boolean
Byte
Character from \u0000 to \u007f (127 in decimal)
Short from -128 to 127
Integer from -128 to 127
I believe this is because the designers of
Java wanted to optimise memory management by having the references refer to the same object. Similar to the
String literal pool I guess.
If anyone has a simple way of remembering this rule, please let me know. Or maybe someone may be able to explain exactly why it is only the above circumstances that apply.