2.1 Identifiers and keywords
2.1.1 Identifier
Syntax rules for identifiers:
- The identifier consists of letters, underscores, dollar signs and numbers, and its length is unlimited.
- The first character of the identifier cannot be a numeric character.
- Identifier cannot be a keyword
- Identifiers cannot be true , false, or null (although true , false, and null are not Java keywords)
For example, the following are all identifiers:
Hello_java、Hello_12$、$23Boy。
It is important to note that the letters in the identifier are case sensitive, and hello and Hello are different identifiers.
2.1.2 Keywords
50 keywords in Java:
2.2 Basic data types
Eight basic data types:
For int type variables, the memory is allocated to 4 bytes ( byte ) . The value range of int type variables is: -2^ 31 ~ 2^ 31 -1
For byte variables, the memory is allocated to 1 byte , occupying 8 bits, so the value range of byte variables is -128 to 127
For short variables, the memory is allocated to 2 bytes , occupying 8 bits, so the value range of short variables is - 2^ 15 ~2^ 15 -1
Long type constants are represented by the suffix L , such as 108L ( decimal ) , 07123L ( octal ) , 0x3ABCL ( hexadecimal )
For the long variable, the memory is allocated to 8 bytes , occupying 64 bits, so the value range of the long variable is -2^ 63 to 2^ 63 -1 .
There is no representation of byte and short constants in Java, but int constants in the range of byte or short can be assigned to byte or short variables.
For char variables, the memory is allocated to 2 bytes, occupying 16 bits, the highest bit is not the sign bit, and there is no negative char . The value range of char type variable is 0~65535 .
The float type constant must have a suffix of f or F memory allocated to 4 bytes, occupying 32 bits. The value range of the float type variable is 1.4E-45 to 3.4028235E38 and -3.4028235E38 to -1.4E-45 .
The float variable retains 8 significant digits when storing float data, and the actual precision depends on the specific value.
The double memory is allocated to 8 bytes, occupying 64 bits. The value range of the double variable is 4.9E-324 ~ 1.7976931348623157E308 and -1.7976931348623157E308 ~ -4.9E-324
The double variable retains 16 significant digits when storing double data, and the actual precision depends on the specific value.
Conversion of basic data types
Sorted from low to high precision:
byte short char int long float double
When assigning a low-level value to a high-level variable, the system automatically completes the data type conversion .
When the value assigned to the variable high-level low-level variables, must use explicit type casts (cast) operation , for example:
for
int x = 1;
byte y ;
y = (byte)x;
Is correct, and y = x; is wrong. The compiler does not check the value of the variable x , only the type of x .
2.3 Array
2.3.1 Declaring an array
Array element type Array name [];
The element type of the array [ ] Array name ;
or
Array element type Array name [][];
The element type of the array [] [] Array name ;
E.g:
float boy[ ];
char cat[ ][ ];
Unlike C/C++ , Java does not allow the number of array elements to be specified within square brackets in the declared array. If declared:
int a[12];
or
int [12] a;
Will cause syntax errors .
2.3.2 Create an array
The format for allocating memory space for an array is as follows:
Array name = new array element type [ number of array elements ];
E.g:
boy= new float[4];
After allocating memory space for the array, the array boy obtained . 4 th used to store float type data memory space (referred to as array elements or units)
The first address of these memory units is stored in the array variable boy , which is called the reference of the array, so that the array can manipulate these memory units by index. The array is a reference variable. The address of the first element of the array is stored in the array variable. The elements of the array are used by adding an index to the array name , such as boy[0] = 1.3F ;
2.3.3 Use of length
For a one-dimensional array , the value of "array name.length " is the number of elements in the array; for a two-dimensional array , the value of "array name.length " is the number of one-dimensional arrays it contains. For example, for
float a[] = new float[12];
int b[][] = new int[3][6];
The value of a.length is 12 ; and the value of b.length is 3 .
2.3.4 Initialization of array
When declaring an array, you can also give an initial value to the elements of the array, such as:
float boy[] = { 21.3f,23.89f,2.0f,23f,778.98f};
The above statement is equivalent to:
float boy[] = new float[5];
then
boy[0] = 21.3f; boy[1 ] = 23.89f; boy[2] = 2.0f;
boy[3 ] = 23f; boy[4] = 778.98f;
You can directly initialize a two-dimensional array with several one-dimensional arrays. The lengths of these one-dimensional arrays are not the same, for example:
int a[][ ]= { {1},
{1,1},
{1,2,1},
{1,3,3,1},
{1,4,6,4,1}
};
2.3.5 Array reference
If two arrays of the same type have the same reference, they will have exactly the same elements. For example, for
int a[] = {1,2,3},b[ ] = {4,5};
Memory model as the shown in FIG .
If the following assignment statements are used ( a and b must be of the same type):
a = b;
Then, a referenced stored and b the same, then the system releases the array originally assigned to a elements such a elements and b of the same elements, a , b memory model becomes as the FIG illustrated.
2.4 Enumeration type
Use the keyword enum to declare enumeration types, the syntax format is as follows:
enum name
{ Constant list
}
For example :
enum Season
{ spring,summer,autumn,winter
}
Declares the name for the Season enumeration type, the enumeration type has 4 Ge constants .
You can declare an enumeration variable with the enumeration name of the enumeration type, for example:
Season x;
An enumeration variable x is declared . The enumeration variable x can only take the value of the constant in the enumeration type. The constant in the enumeration type can be obtained by using the enumeration name and the " . " operator, for example:
x = Season.spring;