The difference between basic types and packaged types

Summary of knowledge points:

简介:
Java has eight basic types, byte, short, int, long, float, double, char, boolean.

Corresponding to eight packaging classes, Byte, Short, Integer, Long, Double, Character, Boolean
insert image description here


The default values ​​of 8 basic data types and the range of data represented:
insert image description here

Summary of knowledge points:

Knowledge summary:

1. The wrapper class is an object, with methods and fields, and the call of the object is by referring to the address of the object;

2. The packaging type is the transfer of reference; the basic type is the transfer of value;

3. The declaration method is different:

Basic data types do not require the new keyword;

The packaging type needs new to allocate memory space in the heap memory

4. Different storage locations:

The basic data type directly saves the value in the stack, and the stack is more efficient;

The wrapper type is to put objects on the heap, and then call them through the reference of the object

5. The initial value is different:

The initial value of int is 0, and the initial value of boolean is false

The initial value of the wrapped type is null

6. Different usage methods:

The basic data types can be directly assigned and used;

Wrapper type is used when collections such as coolingMap

  1. Compare differently
    == and equals

Use == for basic type comparisons, and
equals for wrapper type comparisons.
It is an operator of Java itself. Its function is to compare the addresses of two variables in memory. If they are the same, they are true, and if they are different, they are false.
equals is a method.
In Java, all classes have a root parent class, Object, including wrapper classes.
Without rewriting, direct equals is actually calling the equals method of the Ojbect class.
The equals method of the Ojbect class also uses
to judge.
In other words, equals and == have the same result if not overridden.
And if we want to achieve some other purpose, for example, we want to return true when the address is different but the value is the same, we need to rewrite the equals method ourselves.

  1. Autoboxing and autounboxing

Auto-boxing:
A variable of a basic type becomes a variable of a packaging type, and java will perform the process of auto-boxing and execute the .values() method.
Automatic unboxing:
When you assign a variable of a packaging type to a variable of a basic type Variables, Java will automatically unbox the process and execute the intValue method

9. Principles of use

After the above understanding, it is concluded that the basic classes and wrapper classes should follow the following principles when they are actually used:

1). Try to use the values ​​method. Use the cache as much as possible to improve the efficiency of the program.

2). Class variables use wrapper classes. Imagine an entity class corresponding to a database table. If you use basic data types, some unexpected initial values ​​may be inserted when inserting.

3). The parameters of the method should use the wrapper class. Using a wrapper class means that you can make several parameters null when calling it.
Note: null is meaningless.
But if you use primitive data types, then you need to pass in a value.

4). The return value of the method depends on whether it can be null to determine whether to use the wrapper class or the base class.
When the return value of a method must not be null, it is recommended to use the basic class as the return value.
In this way, when the caller gets the return value of this method, he does not have to worry about it being null.

5). Method internals (local variables) use primitive types. In terms of time efficiency and usage efficiency, basic types are better than packaging types.
Therefore, inside the method, try not to use the wrapper class if you can use the basic type.

In the actual use process, the scenarios are different and the project requirements are different. We need to choose the appropriate type to use according to the actual scenario.

Record: When it comes to the calculation of decimals, the accuracy of the calculation should be considered. You can use BigDecimal, or you can reduce the unit of measurement to achieve the purpose of converting zeros into wholes. The calculation of decimals does not belong to the content of the basic class and the wrapper class.

Guess you like

Origin blog.csdn.net/YHLSunshine/article/details/131252233