9

When we initialize some integer values like

int a = 10; 

or

Integer b = new Integer(20);
Integer b = 30;

where are those objects created in memory?

Is there any concept like Integer-Pool like we have String-Pool for String?

Tordek
  • 10,628
  • 3
  • 36
  • 67
ParagFlume
  • 959
  • 8
  • 17

3 Answers3

4

Most JVMs (even 64-bit ones) use 32-bit references. (Newer JVMs uses 32-bit references for heaps up to almost 32 GB) The reference is on the stack or in a CPU register and is not usually counted. The Integer is allocated on the heap.

Integer i = new Integer(1); // creates a new object every time.
Integer j = 1; // use a cached value.

Integer a; will allocate memory in stack to hold the reference value and initialized with null

new creates instance in heap memory

Piyush Mittal
  • 1,860
  • 1
  • 21
  • 39
  • we know that the code Integer a = 127; is an example of auto-boxing and compiler automatically converts this line to Integer a = Integer.valueOf(127);. So, it is the Integer.valueOf() method that is returning these integer objects, which means this method must be doing something under the hood. _i am confused, Integer a; in heap or stack? – Ulviyya Ibrahimli May 15 '20 at 16:37
  • 1
    https://dzone.com/articles/java-integer-cache-why-integervalueof127-integerva – Ulviyya Ibrahimli May 15 '20 at 16:37
3

Objects created with new keyword are stored on heap.

Variables (references to those objects) and primitive types like int are stored on program's stack.

Integer is not a special class in Java. You can use arithmetic operators with it, it's immutable and you can even use == for equality testing (in the range of -128 to 127 since those values are already cached).

Is there any concept like Integer-Pool like we have String-Pool for String?

Open the code of java.lang.Integer and look at the valueOf method. It's obvious that they indeed use an Integer pool.

public static Integer valueOf(int i) {
    assert IntegerCache.high >= 127;
    if (i >= IntegerCache.low && i <= IntegerCache.high)
        return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);
}

Also, I think this image will be helpful for you:

enter image description here

darijan
  • 9,725
  • 25
  • 38
  • "Objects [...] are stored on the heap." Maybe you should read some words about [_escape analysis_](https://en.wikipedia.org/wiki/Escape_analysis). Actually, this qualifies your statement to be w̶r̶o̶n̶g̶ not entirely true. – Seelenvirtuose Aug 07 '15 at 11:42
  • Yeah, let's go into that to explain it to someone who is learning OO at the moment. We should definitely start with escape analysis and JVM enhancements (before he understands the difference between the stack and the heap). – darijan Aug 07 '15 at 11:46
  • Yes, I know that you should not overwhelm beginners with too much technical details. Nevertheless, this statement is not correct _as it is_. Other readers might be more advanced. A good explanation should be short and correct. Let the readers decide whether to dig into details or not (hyperlinks, heh?). – Seelenvirtuose Aug 07 '15 at 11:49
  • When I see place for improvement in somebody's comment I edit it. For the same reason, i.e. to make them more exact, correct and easy to understand. If you see place for improving my comment, please do so. Thanks for the comments. – darijan Aug 07 '15 at 11:50
-1

Integers in the range -128 to +127 are pre-created. You can think of those as being in an "integer pool".

Any other value is created at runtime.

This leads to some subtle behaviour in Java:

The expression boo == far for two Integer values foo and bar will be true if their values are equal and in the range -128 to +127. It will not be true for values outside that range: you need to use .equals for such values.

Personally I find that pernicious: Never use == on boxed types.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Is this also the case for new Integer(20)? I thought this is only true for boxing. – user23127 Aug 07 '15 at 11:37
  • 1
    Nope. It isn't true for new Integer(20), as that will always create a new object. It's only true for Autoboxing, as this uses the `valueOf` method, where the pre-created values are used. – Florian Schaetz Aug 07 '15 at 11:37