search

Home  >  Q&A  >  body text

java 类型是直接使用基本类型还是就干脆直接用java.lang的类型

比如:

Integer => int
Boolean => boolean
...

黄舟黄舟2771 days ago1108

reply all(14)I'll reply

  • PHPz

    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.

    reply
    0
  • PHP中文网

    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.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 13:08:18

    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

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 13:08:18

    Use basic types as much as possible. If you need to use packaging classes, just convert them

    reply
    0
  • 大家讲道理

    大家讲道理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

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 13:08:18

    Depends on whether you need to use null or not. If necessary, use the packaging type

    reply
    0
  • 迷茫

    迷茫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.

    reply
    0
  • 阿神

    阿神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?

    reply
    0
  • 阿神

    阿神2017-04-17 13:08:18

    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.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 13:08:18

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

    reply
    0
  • Cancelreply