Java's int and Integer

java where everything is an object, the object reference = + object itself.

References are stored in the stack (RAM), because only those who know the life cycle of data can be stored on the stack, objects stored on the heap, because the heap is not required to know the life cycle of the data.

Java, all objects are stored on the heap, when you when new heap space is allocated, but the heap allocation and cleanup slow, if for small simple basic types, with direct reference to the stored value, you can create the stack in.

and so:

1, Integer is an object, which is equal to a reference value +, reference is stored in the stack, the value in the stack; int is a primitive type, the value stored in the references cited in the stack.

2, when the end of the scope, reference will disappear, but stored on the heap objects are affected, is still stored on the heap, so passed by reference, the value is outside the scope of Integer reuse, and int values ​​on with reference gone forever

3, a loss of reference objects are automatically garbage collected

 

Guess you like

Origin www.cnblogs.com/towerbird/p/11574872.html