Data types in Java are divided into basic data types (primitive type) and reference data types.
- There are 8 kinds of basic data types: 4 integer, 2 float, 1 character types for Unicode encoded character cells, char and 1 Boolean types for true truth.
- Reference data types are divided into: classes, interfaces, arrays.
This article mainly describes the basic data types.
# integral type
An integer is used to denote a numeric value without a decimal part, which allows a negative number. Java provides 4 types of data, as shown in the following sections.
In the case of the byte type, the values of the maximum and minimum values are described, and the remaining integral types are similar.
BYTE has 8 bits (where the first bit indicates the sign bit, 0 is positive, 1 is negative), a total of 256 (28) number, divided into positive and negative, is 0 ~ +127 and 1 ~ 128 each 128 numbers.
The maximum value is 0111 1111 and the result is 27-1=127
The minimum value is 1000 0000 and the result is-128
The long integer value has a suffix of l, for example: 4000000000L. If you define this: long num = 4000000000, then the compiler will error: the literal 4000000000 of type int is out of range. It is shown that if the declared integer number does not have a type suffix, it defaults to the int type, and if it is a valid int type, it will be converted to a long type again at long num = a value. So the correct approach should be: long num = 4000000000L;
In Java you can use binary (since Java 7), octal and hexadecimal represent integers.
Prefix the value with:
0B represents a binary value: int x = 0b1001; // 9
0 denotes octal value: int x = 010; // 88 binary is easy to confuse and is not recommended for use.
0x denotes hexadecimal value: int x = 0xCAFE; // 51966
Java Basic data types