The data type

Classification of data types

  • Basic data types

    Byte, short, int, long, float, double, Boolean, char

  • Reference data type

    Array, class, interface, enumeration, annotation

A common base

  • In daily life, the decimal system is used to describe data. The decimal weights are: 10^0, 10^1, 10^2…
  • At the bottom of the computer, the binary sequence of 0 and 1 is used for data description, every two into one, the weight of binary is: 2^0, 2^1, 2^2…
  • The highest (leftmost) bit in binary is used to represent the sign bit, with 0 for non-negative and 1 for negative.
  • Octal and hexadecimal are abbreviations for binary.

Conversion between bases

  • The conversion of positive decimal to binary

    • Mod by 2: use decimal integers to divide the remainder by 2 until the quotient is 0 and then reverse the remainder order.
    • Splitting a decimal integer into the sum of several binary weights, with a 1 below the weight and a 0 otherwise.
  • The conversion of positive binary to decimal

    • Weighting method, which uses each digit in the binary number multiplied by the weight of the current bit and added up.
  • The conversion of negative decimal to binary

    • First convert the absolute value of decimal to binary, then reverse the decimal and add 1.
  • Negative numbers need to be complemented: take the reverse bit and add 1.

  • The conversion of negative binary to decimal

    • Subtract 1 and then invert the decimal number. Add a negative sign to the decimal number.

A range of integers represented by a single byte

  • In computers, a single byte represents eight binary bits, with the highest (leftmost) bit representing the sign bit, 0 representing a non-negative number, and 1 representing a negative number. The range of integers represented is as follows:
  • Non-negative number: 0000 0000 ~ 0111 1111 => 0 ~ 127 => 0 ~ 2^7-1
  • Negative numbers: 1000 0000 to 1111 1111 => -128 to -1 => -2^7 to -2^0
  • The value of a single byte ranges from -2^7 to 2^7-1, that is, **-128 to 127**.

Integer types

  • The Java language describes the types of integer data: byte, short, int, and long. Generally, the int type is used.
  • The byte type contains one byte in the memory space, indicating that the value ranges from -2^7 to 2^7-1.
  • The short type occupies two bytes in the memory space and ranges from -2^15 to 2^15-1.
  • The int type occupies 4 bytes in the memory space, which ranges from -2^31 to 2^31.
  • The long type occupies 8 bytes in the memory space and ranges from -2^63 to 2^63-1.
  • Integer data written directly in Java programs is called a literal/literal/constant and is of type int by default. If you wish to represent a larger immediate quantity, add l or l to the end of the immediate quantity. L is recommended.

Floating point types

  • The types used to describe decimal data in the Java language: float and double. Double is recommended
  • A single-precision floating-point number can represent 7 significant digits in the range of -3.403E38 to 3.403E38.
  • The double type occupies 8 bytes in the memory space. It is called a double precision floating point number and can represent 15 significant digits ranging from -1.798E308 to 1.798E308.
  • Java programs that write decimals directly are called direct quantities. By default, they are of type double. If you want to express direct quantities of type float, you need to add f or f after the direct quantities.

Boolean type

  • The types of information used to describe true and false information in the Java language are: Boolean, with values only true and false.
  • The size of a Boolean type in memory space is not specified, but can be considered 1 byte.

Character types

  • A data type used in the Java language to describe a single character: char. For example: ‘a’, ‘middle’, etc.
  • The char type occupies 2 bytes in the memory space and there is no sign bit. The range of the char type is 0 to 65535. In real life, few data can be described by a single character, so the future development of more strings composed of multiple strings, using the String type to describe, for example: “Hello”, “RG”, etc.
  • Computer the underlying recognition only binary sequence of 0 s and 1 s, the characters’ a ‘such design, does not meet the rules, so the data cannot be directly stored in the computer, but in real life there are design data to computer storage, in order to make the data can be stored can give the data to assign a number, then put the number that can be stored This number is called ASCII.
  • ASCII: ‘0’-48, ‘A’-65, ‘A’ -97, space -32, newline -10
  • Java character types are encoded in the Unicode character set. Unicode is the universal fixed-length character set. All characters are 16 bits.
  • The escape characters required are: “-“, ‘-‘, \-\, \t- TAB, \n- newline

Conversions between basic data types

  • Conversions between basic data types in the Java language: automatic and cast.

  • Automatic type conversion mainly refers to conversion between small types and large types.

  • The cast mainly refers to the conversion from a large type to a small type. The syntax format is as follows:

    Target type variable name = (target type) source type variable name;

  • Strong risk, operation should be cautious!