Java basics: basic types and packaging classes

There are 8 basic types of data in Java, and each basic type has a corresponding packaging class

basic type

There are 8 basic data types in Java, which are shown in the following table:

Basic data type Types of Bytes occupied
byte Numerical 1 byte
short Numerical 2 bytes
int Numerical 4 bytes
long Numerical 8 bytes
float Numerical 4 bytes
double Numerical 8 bytes
char Character type 2 bytes
boolean Boolean  

The space occupied by the basic types

  • Byte (byte) is represented by unsigned 8 bits, and its value range is [-2^7, 2^7-1]. It is the smallest integer type, usually used for network transmission, files or other I/O data streams. The default value is 0.
  • short (short integer) is represented by signed 16 bits, and its value range is [-2^15, 2^15-1]. From daily observations, the short type may be the least commonly used type. It can be replaced by int, because we usually don't need to worry too much about memory capacity. The default value is 0.
  • Int (integer) is represented by signed 32 bits, and its value range is [-2^31, 2^31-1]. The computer stores the two's complement of the integer value. The default value is 0.
  • Long (long integer) is represented by signed 64 bits, and its value range is [-2^63, 2^63-1]. Its literal means it ends with l or L, such as long a = 45454L. The default value is 0L.
  • Float (single-precision floating-point type) is represented by 32 bits and conforms to the IEEE 754 specification. This type can be used if the numerical precision is not high. Literal values ​​of type float usually end with f or F. Since the integer can be automatically converted to the float type, we can also directly assign the integer literal value to the float type variable. The default value is 0F.
  • Double (double-precision floating-point type) is represented by 64 bits and conforms to the IEEE 754 specification. It can represent values ​​with higher precision than float. Double is the highest precision that can be achieved in the basic types of Java. If the requirements are not met, you can use the BigDecimal class in Java. The default value is 0.0.
  • Char (character) is represented by unsigned 16 bits, and its value range is [0, 2^16-1]. Java uses the Unicode character set to represent characters. Unicode maps all known characters in human languages ​​into 16-bit numbers, so the char in Java is 16-bit. The default value is \u00000.
  • Boolean (Boolean) as long as the two literal values ​​of true and false can be used for logical judgment. Boolean can only represent the amount of information of 1 bit, but its size is not precisely defined.

Type conversion
In addition to the boolean type in Java, the other 7 types can be converted between each other. Conversion is divided into automatic conversion and forced conversion. For automatic conversion (implicit), no operation is required, while forced type conversion requires explicit conversion, that is, the use of the conversion operator (type). The 7 types are sorted according to their occupied space:
byte <(short=char) <int <long <float <double
The general rule for type conversion is: small can be directly converted to large, and large to small will lose precision.

Packaging

Each basic type in Java corresponds to a unique packaging class, and the basic type and its packaging class can be converted through static or member methods in the packaging class. The corresponding relationship between each basic type and its packaging class is as follows. It is worth noting that all packaging classes are final modified, that is, they cannot be inherited and rewritten.

Basic data type Packaging
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

Unboxing and packing

      Boxing: the process of converting basic types into packaging classes

      Unboxing: the process of converting packaging classes into basic types

      Provide automatic unpacking and packing after jdk1.5

the difference

  • The basic type is directly used to store the value and is placed in the stack for quick and easy use. The packaging type is a class, and its instance is an object, which is placed in the heap.
  • The basic type is not an object, so there is no method, and the packaging type is a class, so there are methods
  • The basic type can be assigned directly, and the packaging type needs to be created with the new keyword
  • The initial value of the packaging type is null, and the initial value of the basic type is not null, which depends on the type

 The packaging type wraps the value of the basic type in the object, so that the basic type can be manipulated by the method of the object. The conversion between types needs to be completed by the method of the packaging type, so a packaging type is required.

public static void main(String[] args) {
        Integer i1 = 100;
        Integer i2 = 100;
        Integer i3 = 200;
        Integer i4 = 200;
 
        System.out.println(i1==i2);  //①true
        System.out.println(i3==i4);  //②false
}

① the result is true, ② is false

 Integer i1 = 100; The operation performed is 100. First, the boxing operation is performed through the valueOf() method. The source code of Integer.valueOf() is as follows:

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

Integer is special. Between -128 and 127, it will be cached. Numbers that are not in this interval will go to new Integer(i), that is, a new object will be created. Therefore, when 100 is compared, it is true, and when 200 is compared. Is false

 

Conversion between basic types and strings

In program development, we often need to convert between basic data types and strings.

There are three ways to convert basic types to strings:

  1. Use the toString() method of the wrapper class Integer.toString(10)
  2. Use the valueOf() method of the String class String.valueOf(10)
  3. Add an empty string to the basic type, and you get the string corresponding to the basic type data 10 + ""

There are two ways to convert a string into a basic type:

  1. Call the parseXxx static method of the wrapper class Integer.parseInt("10");
  2. Calling the valueOf() method of the wrapper class to convert to a basic type of wrapper class will automatically unbox Integer.valueOf("10");

 

Guess you like

Origin blog.csdn.net/PrisonJoker/article/details/105175870