aspose file tools*
The moose likes Java in General and the fly likes Integer Pool Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Integer Pool" Watch "Integer Pool" New topic
Author

Integer Pool

Surendra Pandey
Greenhorn

Joined: Dec 24, 2009
Posts: 2
Can any one give me the details about Integer Pool? and how it works in Java?
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30516
    
150

Welcome to CodeRanch!

I like this explanation. In a nutshell, Java stores a bunch of integer objects that are used a lot so it doesn't need to keep creating them when you use autoboxing [turn 5 into Integer(5)]


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Rupesh Bonthala
Greenhorn

Joined: Mar 08, 2007
Posts: 5
Integer constant pool is similar to the String Constant pool in Java. Integer constant pool is used to cache frequently used integer objects to increase performance while processing Integers.

Integer.valueOf(int) method provides the Integer from Integer constant pool (if available) rather than creating new object. so Integer.valueOf(int) provides better performance than new Integer(int).
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2286
    
  49
With regard to that article: It is misleading to state that the pool stores values from -128 to 127.
The JLS states that auto-boxing of numbers in the range of -128 to 127 will result in the same object but goes on to say "Less memory-limited implementations might, for example, cache all char and short values, as well as int and long values in the range of -32K to +32K.". Therefore you should not rely on the fact that numbers outside the range of -128 to 127 will be different objects.

Also, you can get the cached Integer objects if you use Integer.valueOf(x) ie:

i1 and i1 are not the same objects whereas i3 and i4 are the same objects.
Phil English
Ranch Hand

Joined: Jun 18, 2012
Posts: 62

Tony Docherty wrote:With regard to that article: It is misleading to state that the pool stores values from -128 to 127.
The JLS states that auto-boxing of numbers in the range of -128 to 127 will result in the same object but goes on to say "Less memory-limited implementations might, for example, cache all char and short values, as well as int and long values in the range of -32K to +32K.". Therefore you should not rely on the fact that numbers outside the range of -128 to 127 will be different objects.

Also, you can get the cached Integer objects if you use Integer.valueOf(x) ie:

i1 and i1 are not the same objects whereas i3 and i4 are the same objects.


Also you can pass a parameter to the JVM on instantiation to force an increase in the upper bound of the integer pool. Discussed in this thread.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30516
    
150

Tony Docherty wrote: Therefore you should not rely on the fact that numbers outside the range of -128 to 127 will be different objects.

True. I think it is good to not reply on caching producing the same objects at all. It produces code that is harder to understand and easy to break by accident. "I'll just change this number from 100 to 100000 and ...."
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
I know it is late to reply, but the simple answer to the problem is to avoid the == operator. I am sure that point was mentioned in the thread Phil English mentioned earlier.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Integer Pool