Introduction to basic data types and their packaging in JAVA

JAVA is divided into basic data types and reference data types
1, basic types (8 types)

  • byte : The smallest data type in Java, which occupies 8 bits (bit) in the memory, that is, 1 byte. The value range is -128~127, and the default value is 0
  • short : short integer, occupies 16 bits in the memory, that is, 2 bytes, the value range is -32768~32717, the default value is 0
  • int : Integer type, used to store integers, which occupies 32 bits internally, that is, 4 bytes, the value range is -2147483648~2147483647, the default value is 0
  • long : Long integer, occupying 64 bits in the memory, that is, 8 bytes -2 63~2 63-1, the default value is 0L
  • float : Floating point type, which occupies 32 bits in the memory, that is 4 bytes, used to store numbers with decimal points (the difference from double is that the float type has only 6 to 7 effective decimal points), the default value is 0
  • double : double-precision floating-point type, used to store numbers with a decimal point, occupies 64 bits in the memory, that is, 8 bytes, the default value is 0
  • char : character type, used to store a single character, occupies 16 bits, ie 2 bytes, the value range is 0~65535, the default value is empty
  • boolean : Boolean type, occupies 1 byte, used to judge true or false (only two values, namely true, false), the default value is false

2. Packaging of basic data types

1. Packaging classes corresponding to basic data types

  • The packaging class corresponding to byte is Byte, and the maximum value, minimum value, type conversion and other functions of byte can be obtained through the packaging object. If a variable of type Byte is declared, the default value of null is not equivalent to the default value of byte 0.

  • Similarly, the long packaging class Long, int packaging class Integer, short packaging class Short

  • The packaging class of char is Character
    . There are three classes in Java responsible for the operation of characters: Character, String, StringBuffer. Among them, the Character class operates on a single character, String operates on a sequence of characters, and StringBuffer operates on a string of characters.
    Char can be automatically packaged into Character; Character can also be automatically unpacked into char.
    But they still have essential differences.

    Character ch2 = new Character(‘A’);

    The default value of the Character type is null when the class is instantiated, because it is a class.

  • Float is a wrapper for float, Double is a wrapper for double, and Boolean is a wrapper for boolean.

2. Introduction to functions covered by packaging

  1. After JDK1.5, Java has realized the automatic unboxing and boxing functions.
    Directly Integer num=12, without using the constructor, that is to say, the basic types and packaging classes can be copied directly, and the unboxing of types are all Jvm is responsible.
    2. The methods implemented in the packaging class are basically the same, here is a brief introduction to the function of the Short packaging class

Short function introduction

Guess you like

Origin blog.csdn.net/u011930054/article/details/87614357