Cache Long in Java

1. Compare two Java Strings
In Java, if you want to compare two String, you know you should use equals instead of ==. To be more specific, here is the normal code to compare two String:

2. Compare two Long
Long class is a wrappers of the primitive type long. The Long class contains a field long which is the value of a Long object and provides user with method to convert a Long object to String.

Comparing two Long should be the same as comparing two String. However, there is something interesting coming up while comparing two Long:

How can comparing n1 and n2 with == returns true and n3 and n4 returns false. What I expected was they should all returns false.

3. Explanation
It turns out that when we work on Long, there is an array of Long objects from -128 to 127 cached. So, anytime we get a Long of value between -128 and 127, such as Long n1 = 100L;, instead of creating a new Long object, it is grasped from cached.

Read Also:  Three levels of GIT config

Source code of the Java Long class:

That is why comparing n1 and n2 returns true. n1 and n2 are actually pointing to the same object. On the other hand, n3 and n4 point to different objects, so the comparison returns false.

The same caching mechanism is applied to Integer, Byte, Character and Short as well. Overall, using == for comparing two Long values is deceptive and we should never use it.

Leave a Reply

Your email address will not be published. Required fields are marked *