Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integers caching in Java [duplicate]

Possible Duplicate:
Weird Java Boxing

Recently I saw a presentation where was the following sample of Java code:

Integer a = 1000, b = 1000;   System.out.println(a == b); // false   Integer c = 100, d = 100;   System.out.println(c == d); // true 

Now I'm a little confused. I understand why in first case the result is "false" - it is because Integer is a reference type and the references of "a" and "b" is different.

But why in second case the result is "true"?

I've heard an opinion, that JVM caching objects for int values from -128 to 127 for some optimisation purposes. In this way, references of "c" and "d" is the same.

Can anybody give me more information about this behavior? I want to understand purposes of this optimization. In what cases performance is increased, etc. Reference to some research of this problem will be great.

like image 893
Beresta Avatar asked Jun 28 '10 09:06

Beresta


People also ask

What is integer caching in Java?

Java Integer Cache Implementation: In Java 5, a new feature was introduced to save the memory and improve performance for Integer type objects handling. Integer objects are cached internally and reused via the same referenced objects. This is applicable for Integer values in the range between –128 to +127.

Where integers are stored in memory in Java?

The Integer is allocated on the heap.

What is caching in Java example?

The Java Object Cache provides caching for expensive or frequently used Java objects when the application servers use a Java program to supply their content. Cached Java objects can contain generated pages or can provide support objects within the program to assist in creating new content.

What is caching mechanism in Java?

The Java Object Cache improves the performance, scalability, and availability of Web sites running on Oracle9iAS. By storing frequently accessed or expensive-to-create objects in memory or on disk, the Java Object Cache eliminates the need to repeatedly create and load information within a Java program.


2 Answers

I want to understand purposes of this optimization. In what cases performance is increased, etc. Reference to some research of this problem will be great.

The purpose is mainly to save memory, which also leads to faster code due to better cache efficiency.

Basically, the Integer class keeps a cache of Integer instances in the range of -128 to 127, and all autoboxing, literals and uses of Integer.valueOf() will return instances from that cache for the range it covers.

This is based on the assumption that these small values occur much more often than other ints and therefore it makes sense to avoid the overhead of having different objects for every instance (an Integer object takes up something like 12 bytes).

like image 53
Michael Borgwardt Avatar answered Sep 18 '22 18:09

Michael Borgwardt


Look at the implementation of Integer.valueOf(int). It will return the same Integer object for inputs less than 256.

EDIT:

It's actually -128 to +127 by default as noted below.

like image 26
dty Avatar answered Sep 17 '22 18:09

dty