2

The following line appears in the allocations pane of eclipse:

Alloc Order       Allocation Size           Allocated Class
509               12                        java.lang.Integer

It references this line of java code:

MyInteger++;

I changed it around a little bit, but it still causes an allocation.

MyInteger=MyInteger+1;
MyInteger=MyInteger+MyIntegerIncrementor;

Why do these lines of code cause an allocation?

Amokrane Chentir
  • 29,907
  • 37
  • 114
  • 158
tjb
  • 11,480
  • 9
  • 70
  • 91
  • 3
    What type of MyInteger? It should be int instead of Integer – Alex Kucherenko May 15 '12 at 15:26
  • for the purposes of this question it is Integer, whether using int or Integer is better depends on the context of your problem. In general you are right and int should be used. – tjb May 15 '12 at 15:47

1 Answers1

3

Since Integer is immutable, each time you increment it a new object is created, hence the allocation.

Amokrane Chentir
  • 29,907
  • 37
  • 114
  • 158