Since the release of Java, the basic data types have been part of the Java language** byte, short, int, long, char, float, double, boolean**.

Among them:

Integer: byte, short, int, long

Character type: char

Float, double

Boolean: Boolean

Before we introduce it in detail, let’s talk about it first. The smallest calculation unit in Java is** byte**, 1 byte = 8 bits.

## 1、 Integer

In Java, integer data belongs to signed number, that is, the first bit with 0 represents positive integer, and the first bit with 1 represents negative integer. In the computer, the negative number is represented by the complement code, so how to calculate the complement?

**Complement code = negative source code + 1;**

For example:

22, which is expressed in the computer as 00010110,

-22, reverse: 11101001, add 1:11101010

## byte

Byte belongs to the integer type in Java, with a length of 1 byte and 8 bits, and the value is 10000000 (- 128) to 011111111 (127). The default value of variable initialization is 0, and the wrapper class byte

## short

Short belongs to the integer type in Java, with a length of 2 bytes and 16bit, with the value of 10000000, 00000000 (- 32768) to 01111111111111 (32767), the default value of variable initialization is 0, and the wrapper class short

## int

Int belongs to the integer type in Java, with a length of 4 bytes and 32 bits. The value is – 2 ^ 31 (- 2147483648) to 2 ^ 31-1 (2147483647). The default value of variable initialization is 0, and the wrapper class is integer

## long

Long belongs to the integer type in Java, with a length of 8 bytes and 64bit, and the value is – 2 ^ 63 (- 9223372036854775808 ) to 2 ^ 63-1 (92233720368547758087). The default value of variable initialization is 0 or 0l, and the wrapper class is long

## 2、 Floating point

In Java, floating-point data can’t be represented directly by binary, but it is an approximate data representation for real numbers. It follows IEEE 754 standard

## float

Float belongs to the floating-point type in Java, also known as single precision floating-point type. Its length is 4 bytes and 32bit. The default value of variable initialization is 0.0F, and the wrapper class float

**1. Float structure**

It consists of three parts*Sign bit, exponent bit and last digit*

Sign bit | Index bit (E) | Last digit (m) | |
---|---|---|---|

length | 1bit | 8bit | 23bit |

explain | 0 is a positive number and 1 is a negative number | The format isThe range of E value is (0255), and the value of index bit is: 0（， | The form is 1. M or 0. M. When e = 0, take 1. M, which is called normal form! =When 0, take 0. M, which is called the irregular form |

**2. Float value**

Formal form:

Informal form:

According to the above formula, it is easy to calculate the value range of float as follows:

(minimum value, when sign bit s takes 1 and exponent bit e takes 255)

(the maximum value, when the sign bit s takes 0 and the exponent bit e takes 255)

The approximate data can be obtained.

Note: there are many special cases according to the different values of the index digit and the last digit, such as Nan, positive infinity and negative infinity, but they are basically not used in ordinary times, so it is no longer in-depth here. At the same time, due to the approximate value, it is not possible to express the amount. It is suggested to use BigDecimal to indicate the amount

## double

Double belongs to the floating-point type in Java, also known as double precision floating-point type. Its length is 8 bytes and 64bit. The default value of variable initialization is 0.0d. The wrapper class is double

**1. Double structure**

It consists of three parts*Sign bit, exponent bit and last digit*

Sign bit | Index bit (E) | Last digit (m) | |
---|---|---|---|

length | 1bit | 11bit | 52bit |

explain | 0 is a positive number and 1 is a negative number | The format isThe range of E value is (02047), and the value of index bit is as follows:（，) | The form is 1. M or 0. M. When e = 0, take 1. M, which is called normal form! =When 0, take 0. M, which is called the irregular form |

**2. Double value**

Formal form:

Informal form:

According to the above formula, it is easy to calculate the value range of double as follows:

(minimum value, when sign bit s is taken as 1 and exponent bit e as 2047)

(maximum value, when sign bit s is taken as 0 and exponent bit e as 2047)

The approximate data can be obtained.

Note: there are many special cases according to the different values of the index digit and the last digit, such as Nan, positive infinity and negative infinity, but they are basically not used in ordinary times, so it is no longer in-depth here. At the same time, due to the approximate value, it is not possible to express the amount. It is suggested to use BigDecimal to indicate the amount

## 3、 Character type

## char

Char belongs to the character type in Java, accounting for 2 bytes and 16bit. It can assign single character and integer values. There is no default value for variable initialization. The wrapper class is character.

For example:

char a = ‘a’;

Char a =’medium ‘;

Char a = 12; / / the value range is 0 ~ 65536, because char type has a corresponding value in the ASCII character code, which can be directly operated to output the corresponding characters in the character table

## 4、 Boolean type

## boolean

There is no special bytecode instruction for Boolean in the JVM. After compiling, the boolean type data will be represented by int type in the JVM. At this time, the Boolean data is 4 bytes and 32 bits, and the Boolean array will be encoded into the byte array of Java virtual machine. At this time, 1 byte of each Boolean data accounts for 8 bits

–Java virtual machine specification

There are only two values of true and false, and the default value of variable initialization is false