java teacher

Autoboxing In Java – Java Integer == Integer – WTF moment with Java

Autoboxing in Java explained with examples

In this post I will try to give you an example showing what autoboxing in Java is. This post is about a curious situation I encountered while debugging a Java application (not written by me). It was quite an “ahhh” “ohhh” “wtf” moment regarding since I spent about an hour or so to figure out. I started investigating an ArrayIndexOutOfBoundsException and realized the the cause was that the index was searched in a piece of code comparing two integers (Integer objects that is) by using Integer == Integer.

Consider the following piece of code:

...
Integer index1 = 100;
Integer index2 = 100;

System.out.println(index1 == index2);

index1 = 200;
index2 = 200;

System.out.println(index1 == index2);
...

What do you think the output will be? If you say false and false, you would be wrong. If you say true and true, you would be wrong again.

The answer is true and false. Quite surprising (to me at least it was).

Well,

I’ve been working intensely with Java for more than 12 years. J2EE applications and customization of the Liferay platform (eg.: Solving Liferay OWASP vulnerabilities).

I have never come across the concept of autoboxing in Java.

I know what you’ll say:

“Then you should have studied harder”. Ok, maybe that’s true. But then, you found this post looking for information about autoboxing, so, it means that you don’t know enough about it either 🙂

In my early days as a Java programmer one of the first things I learned was that comparing objects using “==” is not a good idea since that will compare references, and only if the 2 parts refer the same object then the equality is true. So that’s why in the example above you might think that the output is false in both cases.

Also, whenever I see something like Integer == Integer I tend to change it to Integer.equals(Integer). In the case of the code I was analysing and doing pair-programming, one colleague of mine suggested that Integer == Integer should be the same as Integer.equals(Integer) since the JVM will use autoboxing and transform Integer to int. As I found out that is only partially true.

What Is Autoboxing In Java?

autoboxing in java

The Integer object has its autoboxing mechanism implemented by using a caching mechanism but only for values between -128 and 127, therefore all objects with the value in this interval are referencing the same object. So when you do Integer == Integer JVM will use the cache of the Integer object which is implemented by the Integer’s private class IntegerCache.

Another interesting fact is that you can actually configure the boundaries for this interval. You can do this 2 ways:

1. -Djava.lang.Integer.IntegerCache.high=NEWHIGHVALUE – add this argument to the JVM with the NEWHIGHVALUE with the value of your choice. This will directly configure the Integer’s caching mechanism.

2. -XX:AutoBoxCacheMax=NEWVALUE – this will alter the autobox behaviour of the JVM (autoboxing is the mechanism through which objects are treated as their primitive couterparts – in this case Integer as int); 

To be honest I tried to find some documentation about autoboxing in Java by browsing the java documentation but I wasn’t able to find it.

So, Integer equals Integer but not quite always. Hope you enjoyed this post and I’m looking forwards to your comments and remarks on this topic.

John Negoita

View posts by John Negoita
I'm a Java programmer, been into programming since 1999 and having tons of fun with it.
[jetpack-related-posts]

Leave a Reply

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

Scroll to top