Home >Java >javaTutorial >Integer caching in Java

Integer caching in Java

PHPz
PHPzforward
2023-09-21 22:21:081274browse

Integer caching in Java

Java is one of the most commonly used programming languages ​​today as it includes advanced features and functionality. In every new version of Java, its developers add new features and functionality, and integer caching is a feature introduced in Java 5. In this tutorial, we will understand what is integer cache in Java and its importance in programming. p>

What is integer caching in Java?

From the word "cache", the reader can guess that we are talking about storing an integer in memory and reusing it when needed. Yes, you guessed it. But the question that comes to my mind is why do we need integer cache.

Let us understand how integer caching works and why we need it in Java through sample code.

Example

In the example below, we define the first and second integer variables and initialize them to 100, with a range of -128 to 127. After that, we compare these two variables and based on the comparison result.

Additionally, we define the third and fourth integer variables and initialize them with 130 values ​​that are not in the range -128 to 127. Additionally, we compared them and displayed the resulting values ​​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 "false" output because the "==" operator is used to match references to objects. The "==" operator returns true only if two objects have the same reference. However, if we need to compare object values ​​in Java, we can use equals() method but cannot use '==' operator to match object values.

Thus, it is clarified that "==" returns true when two objects have the same reference, and returns true for a comparison of "first" and "second" objects. This means that the "first" and "second" objects have the same reference.

So, the problem is that 'first', 'second', 'third' and 'fourth' are all integer objects, but it returns true only for comparison of 'first' and 'second', but for 'third' Comparison with "Fourth".

The concept of integer caching is introduced here.

-128 to 127 (inclusive) are the most commonly used integer values. So, when a programmer creates a new integer object with a value in the range -128 to 127, the JVM first checks whether an object with the same value exists in the memory. If so, a reference to the same object is returned. Otherwise, it creates a new object and returns a reference to that object.

Now, it is clear to the programmer why comparing the "first" and "second" objects returns true due to caching.

However, if we create an object using the "new" keyword, it always creates a new object in the memory. Therefore, integer caching does not work in this case.

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

In the above code, the value of the object is between -128 and 127. But when we compare two objects while creating the object using constructor, it returns false.

Benefits of integer caching

  • Memory Optimization - If the object exists, the integer cache will use the same object to store integers between -128 and 127 instead of creating a new object. It saves the memory of the device.

  • Performance - Object creation requires memory allocation and object initialization. Therefore, using existing object creation can improve the performance of your application.

in conclusion

Simply put, the integer cache feature is to cache the most commonly used integer objects in memory for reuse. It improves memory optimization and performance of applications by using existing objects.

The above is the detailed content of Integer caching in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete