Java - Basic data types and encapsulated types

Primitive types have default values, while wrapper types are initially null. Then use these two characteristics for different businesses. According to Alibaba's specifications, all POJO classes must use packaging types, and it is recommended to use basic types for local variables.

ali

 The Java language provides eight basic types. Six numeric types (four integers, two floating point types), a character type, and a Boolean type. 

1. Integer: including int, short, byte, long, the initial value is 0.

2. Floating point type: The initial value of float and double is 0.0

3. Character: The initial value of char is a space, that is, '' ". If output, the effect will not be visible on the Console.

4. Boolean: boolean initial value is false 

basic type size minimum value maximum value
boolean ----- ----- ------
char 16-bit Unicode 0 Unicode 2^16-1
byte 8-bit -128 +127
short 16-bit -2^15 +2^15-1
int 32-bit -2^31 +2^31-1
long 64-bit -2^63 +2^63-1
float 32-bit IEEE754 IEEE754
double 64-bit IEEE754 IEEE754
void

Notice:

The ^ in the table represents the power

Java uses unicode, 2 bytes represent a character, and 1 byte is equal to 16 bits. 

Basic type of wrapper class

Integer 、Long、Short、Byte、Character、Double、Float、Boolean、BigInteger、BigDecmail

Among them, BigInteger and BigDecimal have no corresponding basic types and are mainly used for high-precision operations. BigInteger supports integers of arbitrary precision. 

BigDecimal supports arbitrary precision operations with decimal point

Basic types: int, double, float, long, byte, boolean , char
Packaging types: integer, Double, Float, Long, Byte, Boolean, Char

Characteristics of basic types

1. Basic data types use value transfer when passing parameters.

2. Basic types are created on the stack (there is a leakage problem)

3. Creation is not done through new

Features of package types

1. Create through new or assign directly.

2. When passing parameters, we use "pass by reference", which is the method of address passing.

3. Creation is created on the heap

4. The value of the data type wrapper class is immutable

Advantages of package types:

To create a package type, you can use some methods that the basic type does not have, such as valueOf(), toString(), etc. Also, if you want to pass a reference to an int object instead of a value, you can only use a wrapper class.

Similarities and differences between basic types and package types :

1. In Java, everything is an object, but the eight basic types are not objects.

2. Different declaration methods, basic types do not need to be created through the new keyword, while encapsulated types require the new keyword.

3. Different storage methods and locations. The basic type directly stores the value of the variable, which can be efficiently accessed by saving it on the stack; the encapsulated type needs to point to the instance through a reference, and the specific instance is saved in the heap;

4. The initial value is different. The initial value of the encapsulated type is null, and the initial value of the basic type depends on the specific type. For example, the initial value of the int type is 0, and the boolean type is false;

5. Different usage methods. For example, when working with collection classes, only packaging types can be used.

6. When should you use a wrapper class and when should you use a basic type? It depends on the basic business: whether null values ​​are allowed in this field, if it is allowed, you must use a wrapper class; otherwise, the basic type will suffice. If you use functions such as generics and reflection, you need to use a wrapper class!

Source code

Take a look  Boolean at the properties and constructor to see how it wraps boolean

// final boolean类型的属性,通过构造方法注入值
private final boolean value;
// 构造方法 Boolean a = true 实际上就是调用这个方法
public Boolean(boolean value) {
    this.value = value;
}
// 构造方法
public Boolean(String s) {
    this(parseBoolean(s));
}

To put it simply, the attributes defined by boolean must have a value. If  Boolean the object value is  null, NPE will occur during the unpacking process.

Basic data types and wrapper types use standards

ali

Note:

The assignment method of wrapper classes is valueof, but starting from Java 5.0 (1.5), the JAVA Virtual Machine (Java Virtual Machine) can complete automatic conversion between basic types and their corresponding wrapper classes. Therefore, we use their wrapper classes just like basic types when doing assignments, parameter passing, and mathematical operations, but this does not mean that you can call methods that are only available in their wrapper classes through basic types. In addition, the wrapper classes of all basic types (including void) use final modification, so we cannot inherit them to extend new classes, nor can we override any of their methods.

Take a look at the source code of valueof to understand the caching mechanism

public static Integer valueOf(int i) {
     assert IntegerCache.high>= 127;
     if (i >= IntegerCache.low&& i <= IntegerCache.high)
     return IntegerCache.cache[i+ (-IntegerCache.low)];
     return new Integer(i); 
}

The cache value range of each packaging class:

boolean: true and false 
byte: -128~127 
char: 0~127 
short: -128~127 
int: -128~127 
long: -128~127 
float and double are not cached

The value within the cache range is equivalent to a space that has been opened up, or an object that has been new.

Java--Integer's constant cache pool (default -128~127 value range)_integer cache data range-CSDN Blog

Guess you like

Origin blog.csdn.net/MinggeQingchun/article/details/130796693