Java is a strongly typed language, and each variable can only be declared as one type. However, Java and C + + provide "automatic type promotion" and "forced type conversion" to accommodate some of the different data types of "blending operations." In Java, there are 8 basic data types and 4 reference data types.
One, 8 basic data types
1.1 Integral type
1.1.1 Long Integer type
A long shape is a type used to represent a larger quantity. For example, the number of humans, 6.4 billion, is an integer and requires 8 bytes in the computer to be stored. This is rarely used in ordinary situations.
1.1.2 int Integral type
The int type is much more common than a long integer. In future programming, the most encountered type is the int type. The type of int in Java has a very good feature, that is, the length of the platform-independent, that is, 4 bytes, which brings great convenience to the porting of the program.
1.1.3 Short Type
Short, which saves a memory overhead by comparing the data of the int type. However, even if the short type is capable of expressing data, we still use the int type to represent it. This is because the short type tends to cause compilation errors when "Automatic type promotion": It is possible to lose precision. Another important reason is that, for example, a constant, say 5, in Java, if it appears directly in the expression, the system considers it to be an int type of data.
1.1.4 byte byte type
BYTE-byte data with a small application range.
1.1.5 range of 4 integral types
Type |
Storage space (bytes) |
Range of values |
Long |
8 |
-2^64~+2^64-1 (-92.2 billion-+922 billion) |
Int |
4 |
-2^32~+2^32-1 (-2.1 billion-+21 billion) |
Short |
2 |
-32768~+32767 |
Byte |
1 |
-128~+127 |
PS: Note that all Java data types are signed and there are no unsigned data types
1.2 Floating-point type
1.2.1 Dual Precision Double
The double type, which is used to represent decimals, has a valid number of 15 digits after the decimal point, and can basically meet the requirements of most of the data precision in reality. In Java, if you use decimals, such as 3.14, the default is double type, and if you want to distinguish between float and double, you should add the suffix f (float), D (double) after the declared decimal.
1.2.2 Single Precision Float
The float type, which is also used to represent decimals, but a valid number of decimals is only 7 bits, like int and long, in the actual development process is rarely used to float, and choose to use a double type, so although a bit of memory overhead, but will avoid a lot of compilation errors.
1.2.3 format of floating-point data in hexadecimal notation in Java
In Java, in addition to using 10 binary to represent 3.14, you can also use the 16 binary representation.
10 binary 0.125D
16 binary 0x1.0p-3 (mantissa with 16, exponent using 10, exponent is 2)
PS: Floating-point numbers can be represented by exponents, such as 314.15d==3.1415e2/3.1415e2 (the mantissa uses a 10 binary representation, is a valid number part of the decimal, and e/e represents a 10-based exponent). In addition, it is important that all floating-point data are larger than integer data, where the "big" is not the amount of memory space occupied, but the direction of automatic type promotion. For example, in an expression where the float type data and long integer data are present, the system automatically converts the long type to the float type and then the operation.
1.3 Character type
1.3.1 Unicode-encoded character Char
The difference between the char type of Java and the char type of C + + is that the two correspond to different encodings. C + + corresponds to an ASCII code, and Java corresponds to a Unicode code. The ASCII code occupies only 1 bytes, while the Unicode code occupies 2 bytes. Char in Java can be a character that cannot be represented in Chinese and other regions using ASCII code, and it is clear that the char type in Java is more powerful.
1.4 Two-value
1.4.1 BOOL Boolean
The Boolean value is simple, with only two values: false and True, used to determine the logical condition. in particular, it is important to note that integer values in Java cannot be interchanged with Boolean values.
1.5 Range of values
Type |
Memory space |
Range of values |
Default initialization value |
Double |
8 bytes |
No need to remember |
0.0D |
Float |
4 bytes |
No need to remember |
0.0F |
Char |
2 bytes |
\u0000~\uffff |
\u0000 |
Boolean |
1 bytes |
True/false |
False |
1.6 Automatic Type Promotion
In the figure, 6 solid arrows indicate that the conversion does not result in loss of data information, while 3 open-minded arrows represent conversions that may have a loss of precision .
Before a computer can perform arithmetic operations, it must first turn the operand into the same data type, and then calculate it, corresponding to the mixed data type operation, the Java operation rule is:
- If one of the two operands is of type double, the other operand is automatically converted to a double type;
- Otherwise, if one is a float type, the other operand is automatically converted to the float type;
- Otherwise, if one is a long type, the other operand is automatically converted to a long type;
- Otherwise, the two operands will be converted to the INT type.
Two, 4 types of reference
2.1 Strings String
Java does not have built-in string types. Strings are implemented by providing a predefined string class in the underlying class library through Java.
2.2 Arrays Array
Array, which is a collection of data of the same data type.
Category 2.3 Class
Class classes, which are abstract descriptions of the commonality of objects.
2.4 Interface interface
Interface interface is an interface for extending functionality.
Iii. Summary of data types
Each language has its own data type, and the data type is the commonality of data that is abstracted from the analysis of real-world data. In a computer, each data must correspond to a data type before it can be processed by a computer. These data types are referred to as "built-in data types".
[Java Learning note]-java basic data types