Today's Question:  What weekend projects have you created?        GIVE A SHOUT

Technical Article => Programming =>  Java

What will the value of Integer.valueOf(127) == Integer.valueOf(127) be in Java?

  sonic0002      2018-01-13 22:18:15      4,052    3

Do you really understand how Java does the integer comparison? OK, ignore the statements in the post title. What we are interested in is another set of comparison statements. Let's first see below code snippet.

public class IntegerComparison {
	public static void main(String[] args) {
		Integer a = 127, b = 127;
		Integer c = 128, d = 128;
		System.out.println(a == b);
		System.out.println(c == d);

What do you think the output will be? Are they both displaying true? You will find out the answer after reading through how Java optimizes some of the integer comparison logic.

First, you need to understand the meaning of == operator. In Java, the == operator is used to compare whether two objects are equal in memory address. i.e, whether the two objects are pointing to the same object reference.  The two objects will not be equal if they point to two different object reference even if they have the same content.

Based on above understanding, you would think that both will return false. If you have tried to run the above program, you will be disappointed. No worries, keep reading.

Actually when running Integer a = 127, the primitive integer value 127 will be boxed into an Integer object using


 In Java source code, the implementation of Integer.valueOf() looks like:

public static Integer valueOf(int i) {
	if (i >= IntegerCache.low && i <= IntegerCache.high)
		return IntegerCache.cache[i + (-IntegerCache.low)];
	return new Integer(i);

What is that if block for? Take a closer look and you will find that it compares the value with some lower and upper bound and then retrieve the object from some cache. The key here is the IntegerCache class. This class is a private class of Integer and the value of IntegerCache.low is -128 and IntegerCache.high is some value defined by some configuration(default value is 127). This further means that any value between -128 and 127(by default) will be cached and any new boxing statement will just retrieve the cached object if the value falls between this range. Other values will have a new object created.

With this, you should be able to understand why you see what you see in the output. If no explicit configuration is set. The output will be:


The cache mechanism is kind of optimization because normally the frequency of creating small value is larger than that of creating large value. However, if you don't know about this design, you may see unexpected result at some point of time.

The general rule of using == is to compare the two objects having the same reference. If want to compare the content, please stick to use/override Object.equals() method.


Share on Facebook  Share on Twitter  Share on Google+  Share on Weibo  Share on Reddit  Share on Digg  Share on Tumblr 



Anonymous [Reply]@ 2018-01-14 07:16:19

The comparison method is "equals" instead of "equalsTo". There's also the Objects.equals for method referencing and avoiding NPE. MessageDigest.isEqual is used to compare hashing such as SHA-256 not object instances. 

Ke Pi [Reply]@ 2018-01-14 07:57:48

Thank you for pointing the silly mistakes I made. Have corrected them. Need to review Java basics again now :(

Anonymous [Reply]@ 2018-01-22 19:33:48

good,i get it ,thanks


Just before deploying to product server

By sonic0002