I. Java
1. variable definition:A data item defined by an identifier, including the type, name, and value.
2. Variable Classification:
Type |
Size |
Description |
Byte |
1 byte (8 bits) |
Range:-128 ~ 127 is commonly used in byte arrays. For example, you can store numbers in byte arrays (see Exercise 2) and convert strings into byte arrays (see CODEH ). |
Short |
2 bytes (16 bits) |
Range:-32767 ~ 32768 |
Int |
4 bytes (32 bits) |
Int is the default Integer type, that is, all initialized integer types are int, such Byte I = 22; byte J = 3; I + J and 5 are not byte type but int type |
Long |
8 bytes (64-bit) |
If an integer is defined as long, it must be followed by L or l, for example, 256l. |
Float |
4 bytes (32 bits) |
Floating-point number type. If an integer or floating-point is defined as the float type, then f is added. Because the default floating-point type is double, when the integer is assigned to the float, if the number of integer digits exceeds 32, F is required; otherwise, F is acceptable. |
Double |
8 bytes (64-bit) |
The default type of the floating point type, but the double type can be added with D or not |
Char |
2 bytes (16 bits) |
It is used to store characters, indicating that the characters in the Unicode encoding table occupy two bytes. The English characters and numbers only occupy one of the bytes while the Chinese characters occupy two bytes, char can be converted to int. You can assign a value of ''but not''. You can use ASCII code to identify characters. The value range is 01 ~ 255. ASCII code is often used to determine the range of characters. |
Boolean |
1/8 bytes (1 bit) |
There are two values: true and false, which can be exchanged with 0 and 1. The minimum addressing space of the boolean type is 1 byte, which occupies 1 byte, but only occupies 1 of them. |
Note: Although float and INT have the same number of digits and the double and long have the same number of digits, both int and long have no float
And double indicate a large range, because double and float have decimal places.
3. Data Type Conversion
Note:
Smaller types can be directly assigned to larger types, such as byte I = 8; Int J = I; larger types must be forcibly converted to smaller types, for example, int M = 256; byte n = (INT) m; however, the precision is lost because byte stores up to 8 bits, but if M does not exceed 8 bits, the precision is not lost. The figure is as follows:
256: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 (32 bits)
Forced conversion to byte type only 8 digits 0 0 0 0 0 0 0 0 0 0 is changed to 0, at any time precision, the same is true for other numeric types (including char converted to numeric type ).