PHPz2017-04-17 13:08:18
int is a basic type, and Integer is an object. This is the essential difference between the two. See the figure below for specific usage.
PHP中文网2017-04-17 13:08:18
Use specifically in specific situations.
Generally, the basic types of beans are enough.
If the field involves an object. Then use packaging type.
PHP中文网2017-04-17 13:08:18
Two principles:
Use boxed type when you need to use objects, try to use unboxed type where objects are not needed
Convert boxed <==> unboxed as little as possible throughout the program. In other words, convert only when conversion is necessary
ringa_lee2017-04-17 13:08:18
Use basic types as much as possible. If you need to use packaging classes, just convert them
大家讲道理2017-04-17 13:08:18
A lot has been said above, let me tell you one more important point,
Long i, long i are incremented respectively, and run one million times respectively, you will find that the time required is very, very different!
This is because a new Long object is constructed each time to install the long instance
PHP中文网2017-04-17 13:08:18
Depends on whether you need to use null or not. If necessary, use the packaging type
迷茫2017-04-17 13:08:18
Use native types as much as possible. If you need to pass a reference or put it into a container, use a wrapper.
阿神2017-04-17 13:08:18
The overhead of storing an Integer type in 32-bit systems:
32-bit storage object reference
32-bit storage object tag information: object status, etc.
Lock information for 32-bit storage objects
32-bit storage of int value information
In other words, initializing an Integer object requires 128 bits of memory space
A common int type data only requires 32 bits of information.
In this case, what do you think should be the priority?
阿神2017-04-17 13:08:18
Use basic types unless necessary.
天蓬老师2017-04-17 13:08:18
Generally, basic types are used directly, and encapsulation is rarely used unless necessary