2.1 Data types
java represents an integer constant, it is int by default, and if the integer constant is preceded by a 0, the octal number If there is a ' 0x ' or ' 0X ' in front of the integer constant, it represents hexadecimal.
long type constant must be followed by an ' l ' or ' l '. When output with the System.out.println () method, the prefix name is not output.
Note: Do not assign a double type constant to a variable of type float, It is obviously wrong to assign a double with a length of 8 bytes to a constant of 4 bytes.
java character is Unicode encoded, each character occupies two bytes, and the ASCII code (0~ 127) is also included in the Unicode code.
Assign an integer to a character variable, then use the SYSTEM.OUT.PRINTLN () The output is automatically converted to characters instead of integers. A character variable can perform arithmetic operations.
2.2 Constants (unlike the C language, there are no unsigned numbers in Java )
There are five types of constants in Java, such as Integer, float, character, Boolean, and string constants.
Identifiers are valid sequences of characters used to identify variable names, function names, array names, file names, class names, and so on. Note You cannot start with a number.
Note the use of escape characters. In particular, when adding single or double quotation marks to a string, you must add a \ to the quotation mark you want to include, which is the same as the use of the escape character. That is, the escape character can be used alone as a character constant or in a string constant.
2.3 Variables
The scope of a variable is valid only for the curly brace range where the variable is located.
In an expression in which a variable participates in an operation, because its median value may exceed the representation of any one of the operand types, Java automatically promotes the numeric type of the participating operation for the sake of prevention. For example:
public class sample2_14{ static void main (string[] args) {short a = 200; short b = 10; short C = A * b; // System.out.println (c); }}
The data type of a and B is promoted to int, so the expression outputs an int rather than a short type.
Java defines the type-promotion rules for expressions:
- If an expression has an operand of byte or short, the result of the expression is int;
- If there is an operand of long in the expression, the result of the expression is long;
- If there is an operand of float in the expression, the result of the expression is float;
- If an expression has an operand of double, the result of the expression is double;
When you assign an integer to a float variable or a double variable, and then output a variable of floating-point type to the System.out.println () method, its value is added after the integer value. 0.
2.4 Conversions between data types
Automatic type conversion is also called implicit type conversion, that is, the system does not require the user to specify the target data type. This conversion must meet the following conditions:
- Two types are compatible with each other
- The range of the target data type value is greater than the value of the converted data type
If both of these conditions are not met, this requires coercion of type conversions. For example, when assigning the value 300 of an int type variable to a byte type variable, a variable of type int is cast to a byte type variable, at which point the value of the INT type variable is modeled with the range of the value of the byte type, that is, Mod 256 = 44, the conversion is complete. When a floating-point number is converted to an integer, the fractional part is discarded, and the integer portion of the conversion is the same as above.
The 2nd chapter of the Java Development Handbook learning process basic data types