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
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?
Avoid problems caused by type conversion (especially some implicit conversions that increase the difficulty of troubleshooting). For example, problems such as missing intensive reading after conversion and null pointers.
Basic types are stored on the stack and have fast reading and writing speeds. Although the speed is not obvious.
Basic types are passed by value. The advantage is that when passing parameters, the status value of the parameters is rewritten inside the method and does not affect external parameters. And it is easier to control in concurrency.
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.
Use specifically in specific situations.
Generally, the basic types of beans are enough.
If the field involves an object. Then use packaging type.
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
Use basic types as much as possible. If you need to use packaging classes, just convert them
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
Depends on whether you need to use null or not. If necessary, use the packaging type
Use native types as much as possible. If you need to pass a reference or put it into a container, use a wrapper.
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?
Use basic types unless necessary.
Generally, basic types are used directly, and encapsulation is rarely used unless necessary