Integer object caching

xx:autoboxcachemax
integer caching python
integer pool java
autoboxing
java string cache
box integer
java integer range
how to convert primitive int to integer object in java

I read that "So when creating an object using Integer.valueOf or directly assigning a value to an Integer within the range of -128 to 127 the same object will be returned."

This is the reason why :-

Integer a=100;
Integer b=100;
if(a==b) // return true as both the objects are equal

But why not in this below case? These two values are also in the range of 127 and -128 so according to the statement above this two will also return the same objects.

But the output here i am getting as "Not"

public static void main(String[] args) {

    Integer a = 10;
    Integer b = 12;
    if(a == b)
        System.out.println("same");
    else
        System.out.println("Not");
}

Can someone explain?


You're misunderstanding what "the same object will be returned" means.

So, comparison with == is actually comparing memory locations, and returns true only when the two variables hold the same object (i.e. stored at the same memory location).

Values between -128 to 127 are stored in the integer constant pool, which means that every 10 is the same 10 (i.e. the same memory location), every 12 is the same 12, etc. But it's not the case that all 10s are also 12s, which is what your question is unintentionally assuming.

Anyway, once you get outside of that range, each primitive int is a new object and is assigned to a new memory location outside of the constant pool.

You can test that with the following code:

public static void main(String[] args) {

    Integer a = 1000;
    Integer b = 1000;
    if(a == b)
        System.out.println("same");
    else
        System.out.println("Not");
}

That will print "Not", because a and b are two different objects stored in different memory locations.

And this is why you should compare things with .equals()

Java Integer Cache: Why Integer.valueOf(127) == Integer.valueOf , Instances of Integer and other wrapper classes are cached by the JVM for increased performance. Read this post and learn how you can tweak  Integer objects are cached internally and reused via the same referenced objects. This is applicable for Integer values in range between –127 to +127 (Max Integer value). This Integer caching works only on autoboxing. Integer objects will not be cached when they are built using the constructor.


== Checks whether both the references are pointing to the same memory location. In the first case both values are same so they are pointing to the same location only one object will be created.

Integer a=100;
Integer b=100;
if(a==b) // return true as both the objects are equal

In the second case both values are different so they have different memory location for each so two objects will be created.

public static void main(String[] args) {

    Integer a = 10;
    Integer b = 12;
    if(a == b)
        System.out.println("same");
    else
        System.out.println("Not");
}

The Internal Cache of Integers, Basically, the Integer class keeps a cache of Integer instances in the of having different objects for every instance (an Integer object takes up  What is small integer caching? Python caches small integers, which are integers between -5 and 256. These numbers are used so frequently that it’s better for performance to already have these objects available. So these integers will be assigned at startup. Then, each time you refer to one, you’ll be referring to an object that already exists.


If you read the actual Java doc, you'll see a clearer description of what it is actually doing

Returns an Integer instance representing the specified int value. If a new Integer instance is not required, this method should generally be used in preference to the constructor Integer(int), as this method is likely to yield significantly better space and time performance by caching frequently requested values. This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.

As the Integer returned must represent the specified int value, there is no possible way that

Integer a = 10;
Integer b = 12;
System.out.println((a==b));

will print "true", as clearly the same object couldn't represent both values.

Edit:

For the sake of precision - The Java standard doesn't require that Integer autoboxing (assigning a primitive int to an Integer object) uses Integer.valueOf(), so it is quite possible that in a conforming Java implementation

Integer a = 10;
Integer b = 10;
System.out.println((a==b));

will print "false";

Integers caching in Java, return new Integer(i); }. As correctly inferred, this caching code was added to support the object identity semantics of autoboxing for values  The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int.. In addition, this class provides several methods for converting an int to a String and a String to an int, as well as other constants and methods useful when dealing with an int.


Simply They are not same object, those are two different Integer instances for to hold the given value, so as if the object are different it will always print Not

Boxing and Caches: Integer.valueOf(int) and friends, These numbers are used so frequently that it's better for performance to already have these objects available. So these integers will be assigned at startup. Then,​  Java caches integer objects that fall into -128 to 127 range because this range of integers gets used a lot in day-to-day programming, which indirectly saves some memory.


Small Integer Caching – Real Python, The answer is twofold: is checks whether 2 things are the same object, not if they'​re equal; Python caches integers in the range [-5  The short answer to this question is, direct assignment of an int literal to an Integer reference is an example of auto-boxing concept where the literal value to object conversion code is handled by the compiler, so during compilation phase compiler converts Integer a = 127; to Integer a = Integer.valueOf(127);.


Python WAT!? Integer Cache, He is not caching ints, he's caching the Integer object representation of an int. If he was regularly using loads of Integer objects for values below 256, this might  Method 4, Integer p = Integer.valueOf(1); is the recommended way. The JavaDoc says: Returns an Integer instance representing the specified int value. If a new Integer instance is not required, this method should generally be used in preference to the constructor Integer(int), as this method is likely to yield significantly better space and time performance by caching frequently requested values.


The Integer Cache, Java caches all the Integer objects formed from primitive int values between values -128 to 127 (including -128 and 127). This caching is done in Integer.​valueOf  Integer Objects¶ All integers are implemented as “long” integer objects of arbitrary size. On error, most PyLong_As* APIs return (return type)-1 which cannot be distinguished from a number. Use PyErr_Occurred() to disambiguate. PyLongObject¶ This subtype of PyObject represents a Python integer object. PyTypeObject PyLong_Type¶