java 类型是直接使用基本类型还是就干脆直接用java.lang的类型
黄舟
黄舟 2017-04-17 13:06:18
0
14
1147

比如:

Integer => int
Boolean => boolean
...

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(14)
PHPzhong

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.

Ty80

Use specifically in specific situations.

Generally, the basic types of beans are enough.

If the field involves an object. Then use packaging type.

Ty80

Two principles:

  1. Use boxed type when you need to use objects, try to use unboxed type where objects are not needed

  2. 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.

  1. 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.
  2. Basic types are stored on the stack and have fast reading and writing speeds. Although the speed is not obvious.
  3. 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.
Peter_Zhu

Generally, basic types are used directly, and encapsulation is rarely used unless necessary

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template