Whether the java type uses the basic type directly or simply uses the java.lang type directly.

for example:

Integer = > int
Boolean = > boolean
.

Mar.11,2021

int is the basic type, and Integer is the object, which is the essential difference between the two. The specific use is shown in the figure below.


it is used in specific situations.

in general, the basic types in bean are fine.

if the field involves an object. Then use the packing type.


two principles:

  1. use the boxed type when you need to use objects, and use unboxed types where you don't need them as much as possible

  2. in the whole program, do boxed < = = > unboxed conversions as little as possible, in other words, convert

  3. only when you have to.

you can use basic types as much as possible. If you need to use wrapper classes, you can convert them

.

basic types are always used unless necessary.

  1. avoid the problems caused by type conversions (especially some implicit conversions make it more difficult to troubleshoot). For example, there are some problems, such as lack of intensive reading after conversion, null pointer and so on.
  2. basic types are stored in the stack with fast reading and writing speed. Although this speed is not obvious.
  3. basic types are passed by value. The advantage is that when passing parameters, the state value of the parameters is overwritten inside the method, so that the external parameters are not affected. And it is also easier to control in concurrency.

it's all said a lot, and I'm going to make a more important point,

.

Long iMagin long I is added and run a million times respectively, and you will find that the time required is very, very different!
This is due to the construction of a new Long object loading long instance at a time during Long object operations


see if you need to use null. No, just use the wrapper type.


try to use native types. Use a wrapper if you need to pass a reference or put it in a container.


the cost of storing an Integer type in a 32-bit system:
32-bit storage object reference
32-bit storage object tag information: object status, etc.
Lock information for 32-bit storage objects
32-bit store int value information
That is, initializing an Integer object requires 128bit memory

A normal int type data requires only 32 bits of information.

in this case, what do you think is your priority?


generally use basic types directly and rarely use encapsulation, unless necessary


divide into different scenes


jdk 1.5. There is a mechanism of automatic unpacking and packing. In order to cater to the core idea of java, everything is an object.
So in development, if the performance requirements are not very high, it is recommended to use the wrapper class in the java.lang package.


according to specific needs, but I always give priority to using the basic type, because it takes up less space than the wrapper type, and their construction speed is faster than the wrapper type. Comparing the wrapper type is an object, and secondly, for the wrapper class, it provides a static method to construct the wrapper type from the basic type, so if you need to use the wrapper type, you can convert it immediately.


java primitive type is a variable, Integer is a wrapper class for java, and wrapper classes are used in generics

Menu