<div dir="ltr" style="text-align: justify;" trbidi="on">In Java auto-boxing is very interested feature which automatically covert int to Integer, long to Long, float to Float etc. This feature introduced was introduced in the JAVA 5. Any developer want to compare the int to int or int to Integer or Integer to Integer, they mostly use == operator. But this operator == might be cause of many subtle bugs in your application because it works only for some defined range of integers. In some cases this operator == might be cause of NullPointerException due to whenever we will compare int to Integer, the Integer object is converted to a primitive value, if the Integer object is null, it throws NullPointerException. So when you compare two Integer objects using a == operator, Java doesn&#8217;t compare them by value, but it does reference comparison that means even if the two integer has the same value, == can return false because they are two different objects in the heap. That&#8217;s why you should use equals() method to compare objects. </p>
<div align="center" id="ads-id"></div>
<p>
This is one of very weird java question that will make your head spin. Java does it by caching of integer objects by Integer.valueOf() method. That mean your code might be work some range of integers but not for others. So what is this range of integers, Integer object for range -128 to 127, it return same Integer object in heap, and that&#8217;s why == operator return true if you compare two Integer object in the range -128 to 127, but return false afterwards, causing bug. This happens because auto boxing uses Integer.valueOf() method to convert Integer to int and since method caches. </p>
<div style="background-color: #f2f9fc; border:1px solid #c9e6f2;border-radius:3px;padding:16px;line-height:1.45;"><b>Popular Tutorials</b></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/2012/06/spring-30-baby-step-to-learn.html"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/spring-web-mvc-framework-chapter-38.html"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/2016/06/introduction-to-spring-boot-a-spring-boot-complete-guide.html"><em><strong>Spring Boot Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/2013/02/spring-security-take-baby-step-to-secure.html"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/2012/07/introduction-to-aop-in-spring.html"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/using-spring-jdbc-framework-chapter-32.html"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/2017/01/spring-hateoas-hypermedia-driven-restful-web-service.html"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/2017/01/microservices-with-spring-boot.html"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/2013/06/jax-rs-web-service-tutorial.html"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/2013/01/core-java-baby-step-to-be-best-java-ian.html"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/2012/03/hibernate-3-on-baby-steps.html"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/spring-batch-process-with-example.html"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<p>
There is no such problem if you using pre or Java 1.4 version because there was no auto boxing, so you always knew that == will check reference in heap instead of value comparison.</p>
<p><b>Example</b><br />
Let&#8217;s see below line of code about this problem.</p>
<pre class="highlight">public class Main {
 public static void main(String[] args) {
 
 Integer c = 666;
 Integer d = 666;
 System.out.println("c == d "+ (c == d));
 Integer a = 42;
 Integer b = 42;
 System.out.println("a == b "+ (a == b));;
 
 }
}

</pre>
<p>
<b>Output</b><br />
<i>c == d false<br />
a == b true</i></p>
<p>According to the output of above program c and d have value 666 but == operator return false and a and b also have same value 42 but here == operator return true.</p>
<p>In first case the literal 666 is boxed into two different Integer objects and then those objects are compared with ==. The result is false, as the two objects are different instances, with different memory addresses. Because both sides of the == expression contain objects, no unboxing occurs.</p>
<p>In the second case int values from -127 to 127 are in a range which most JVM will like to cache so the VM actually uses the same object instance for both a and b. As a result, == returns a true result.<br />
<b><br />
</b> <b>Summary</b></p>
<p>So we have to avoid when comparing two integer objects by == operator. We can equal() method to compare the integer objects. The equal() method always compare the value of integer objects instead of comparing its references.</p>
<div style="background-color: #f2f9fc; border:1px solid #c9e6f2;border-radius:3px;padding:16px;line-height:1.45;"><b>String Related Posts</b></p>
<ol style="text-align: left;">
<li><b><a href="https://dineshonjava.com/2014/11/how-to-make-java-class-immutable.html">How to make Immutable Class in Java</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/core-java-interview-questions.html">Core Java Interview Questions</a></b></li>
<li><b><a href="https://dineshonjava.com/2013/02/string-class-in-java.html">String Class in Java</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/why-string-is-immutable-and-final-in-java.html">Why String is Immutable and Final in Java</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/convert-string-to-integer-to-string-in-java-with-example.html">Convert String to Integer to String in Java with Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/differences-between-string-stringbuffer-and-stringbuilder-in-java.html">Differences between String, StringBuffer and StringBuilder in Java</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/difference-between-stringbuilder-and-stringbuffer-in-java.html
">Differences StringBuffer and StringBuilder</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/hashcode-and-equals-methods-in-java.html
">Difference between hashCode() and equals() methods in Java<br />
</a></b></li>
<li><b><a href="https://dineshonjava.com/2013/03/java-string-substring-method.html
"> Java &#8211; String substring() Method<br />
</a></b></li>
<li><b><a href="https://dineshonjava.com/2013/05/difference-between-wait-and-sleep.html
"> Difference between wait() and sleep()<br />
</a></b></li>
</ol>
</div>
<p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/how-to-remove-duplicate-items-from-arraylist-in-java/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/why-string-is-immutable-and-final-in-java/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '75'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…