Basic Data Type-knowledge point summary, data type-knowledge point
Java is a strongly typed language. A strong type contains two meanings: ① All variables must be declared and used before use; ② variables of the specified type can only accept values matching the type.
Java supports two types:
LBasic Type: IncludesInteger type,Floating Point Type,Boolean Type,Character Type.
LReference Type: IncludesClass,Interface,Array typeAndNull type. The reference data type refers to the reference of an object, which includes instances and arrays.
1. Basic data type classification 1.1. Integer
An integer is used to indicate a value without a decimal part. It can be a negative number. Java provides four types of integers.
Type |
Storage Requirements |
Byte |
1 byte |
Short |
2 bytes |
Int |
4 bytes |
Long |
8 bytes |
Java contains four integer Representation Methods: decimal, binary, octal, and hexadecimal.BinaryTo0bOr0B(Binary support is added after Java 7 );OctalTo0Start,HexadecimalTo0xOr0XStarting from 10 ~ 15 respectively ~ F (Case Insensitive.
It also starts from Java 7 and can also be underlined by the literal number, for example, 000_000 to represent 1 million. These underscores are only used to make reading easier. The Java compiler removes the underlines.
1.2. Floating Point Type
The floating point type is used to indicate a number with a decimal part.
Type |
Storage Requirements |
Float |
4 bytes |
Double |
8 bytes |
Double indicates that the precision of this type of value is twice that of the float type (someone calls it a double-precision value ).Most applications use the double type.In many cases, float type precision is difficult to meet the requirement.
Float TypeThe value of hasSuffix F(For example, 3.14F ). Without the suffix FThe default value of floating point is double.. Of course, you can also add the suffix D (for example, 3, 14D) after the floating point value ).
The floating point number in Java can be expressed in two forms.
L decimal number form: This form is a simple floating point number, such as 3.15, 3.0,. 3. A floating point number must contain a decimal point. Otherwise, it will be treated as an int type.
L scientific Notation: for example, 3.15e2 (3.15x102) and 3.15E2 (also 3.15x102 ).
It must be noted that,Only floating-point values can be expressed in scientific notation.. For example, 3200 is an int value, but 32E2 is a floating point value.
1.3. char type
Char represents a single character ., Character Type values must be enclosed in single quotes. Storage requirement: 2 bytes.
1.4. boolean type
Boolean has two values: false and true, which are used to determine logical conditions. Integer and boolean values cannot be converted to each other.
2. type conversion of basic data types 2.1. Automatic type conversion
When a value or variable with a small table number range is directly assigned to a variable with a large table number range, the system can automatically convert the data type; otherwise, forced type conversion is required.
Conversion direction:Byte --> short/char --> int --> long --> float --> double
In addition, when any basic type value and string are connected, the value of the basic type is automatically converted to the string type, although the string type is not a basic type, but a reference type. Therefore, if you want to convert the value of the basic type to the corresponding string, you can connect the value of the basic type with an empty string.
2.2. Forced type conversion
If you want to reverse convert the direction of automatic conversion, You need to force type conversion.
Forced type conversionSyntax format:(TargetType) value, The operator number for forced type conversion is parentheses (()).
3. Automatic Upgrade of expression type
When an arithmetic expression contains values of multiple basic types, the Data Type of the entire arithmetic expression is automatically upgraded. Java defines the following automatic upgrade rules.
L all byte, short, and char Types will be upgraded to the int type.
L the Data Type of the entire arithmetic expression is automatically upgraded to the same type as the highest-level operand in the expression.