Java Integer Cache


Java is one of the most used programming languages nowadays, as it contains advanced features and functionalities. In every new version of Java, its developers add new features and functionalities, and an integer cache is one feature introduced in Java 5. In this tutorial, we will understand what integer cache is in Java and the importance of that in programming.

What is Integer Cache in Java?

From the 'cache' word, readers can guess that we are talking about storing integer in the memory and reusing it whenever required. Yes, you have guessed correctly. But the question comes to mind is that why we required integer caching.

Let's understand via example code how Integer Cache works and why we required it in Java.

Example

In the example below, we define the first and second integer variables and initialize them with 100, which is in the range of -128 to 127. After that, we compared both variables and printed the true or false value according to the comparison result.

Also, we have defined the third and fourth integer variables and initialized them with 130 values which are not in the range of -128 to 127. Also, we have compared them and shown the resultant value in the output.

import java.io.*;

public class Main {
   public static void main(String[] args) {
      Integer first = 100;
      Integer second = 100;
      
      // It prints true only when both objects have the same reference
      if (first == second) {
         System.out.println("true");
      } else {
         System.out.println("false");
      }
      Integer third = 130;
      Integer fourth = 130;
      if (third == fourth) {
         System.out.println("true");
      } else {
         System.out.println("false");
      }
   }
}

Output

true
false

If we guess the output, both should return the 'false' output as the '==' operator is used to match the object's reference. The '==' operator returns true only when both objects have the same reference. However, if we need to compare the object values in Java, we can use the equals() method, but we can't use the '==' operator to match object values.

So, it is clarified that '==' returns true when both objects have the same reference and returns true for the 'first' and 'second' objects' comparison. It means the 'first' and 'second' object has the same reference.

So, the question is that 'first', 'second', 'third', and 'fourth' all are integer objects, but it returns true only for the comparison of the 'first' and 'second' but not for the comparison of the 'third' and 'fourth'.

Here, the concept of the Integer cache comes into the picture.

The -128 to 127 inclusive are the most used integer values. So, When the programmer creates a new integer object with a value in the range -128 to 127, the JVM first checks whether the object with the same value exists in the memory. If yes, it returns the reference of the same object. Otherwise, it creates new objects and returns the reference of that.

Now, it is clear to programmers why the comparison of the 'first' and 'second' object returns true due to caching.

However, if we create the object using the 'new' keyword, it always creates a new object in the memory. So, the integer cache doesn't work in this case.

Integer first = new Integer(13);
Integer second = new Integer(13);

In the above code, the value of objects is between -128 to 127. Still, it will return false when we compare both objects as we created the object using the constructor.

Benefits of the Integer Cache

  • Memory optimization − The integer cache uses the same object for integers between -128 to 127 if the object exists rather than creating a new object. It saves the memory of the device.

  • Performance − The object creation requires memory allocation and initialization of objects. So, creating using the existing object improves the performance of the application.

Conclusion

In simple terms, the integer cache feature is used to cache the most used integer objects in the memory for reuse. It improves the application's memory optimization and performance by using the existing objects.

Updated on: 24-Jul-2023

131 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements