Basic syntax of java (the most complete in history)
(1) Keywords and reserved words
Definition and characteristics of keywords
Definition: A string given a special meaning by the Java language and used for special purposes.
Features: All letters in keywords are lowercase. Keywords cannot be used as variable names, method names, class names, package names and parameters.
2.Reserved words
Definition: Not used in Java now, but may be used as a keyword in future versions. It should be avoided when using it on its own.
(2) Identifier
identifier
Any place that can be named by itself is called an identifier. For example: package name, class name, method, etc.
Define legal identifier rules
1. There are 26 English letters in uppercase and lowercase, 0-9, _ or $.
2. Cannot start with a number.
3. Keywords and reserved words cannot be used, but keywords and reserved words can be included.
4. Strictly case-sensitive.
5. The identifier cannot contain spaces.
Name naming convention in java
1. Package name: When it consists of multiple words, all letters are lowercase: xxxyyyzzz
2. Class name, interface name: When composed of multiple words, the first letters of all words are capitalized: XxxYyyZzz
3. Variable names and method names: When composed of multiple words, the first letter of the first word is lowercase, and the first letter of each word starting from the second word is capitalized: xxxYyyZzz
4. Constant names: All letters are in capital letters. When there are multiple words, connect each word with an underscore: XXX_YYY_ZZZ
(3) Variables
concept of variables
1. A storage area in memory.
2. The data in this area can continuously change within the same type range.
3. Variables are the most basic storage units in programs. Contains variable type, variable name and stored value
The role of variables
Used to save data in memory.
variable declaration
Syntax: <data type> <variable name>
int a;
Assignment of variables
Syntax: <variable name> = <value>
a = 10;
Note: Every variable in Java must be declared first and then used.
Variable declaration and assignment
Syntax: <data type> <variable name> = <initialization value>
int a = 10;
Variables declared outside a method and inside a class are called member variables.
Variables declared inside a method body are called local variables.
The similarities and differences between the two in terms of initialization
Same: both have life cycles. Exception: Local variables, except formal parameters, need to be initialized.
(4) Basic data types
Automatic type conversion (implicit)
1. Features: The code does not require special processing and is completed automatically.
2. Rules: Data range from small to large
For example: int type is automatically converted to long type
long b = 10;
Notice:
1.byte, short, and char are not converted to each other. They are first converted to int type during calculation.
2. The boolean type cannot be operated with other data types.
3. When concatenating a value of any basic data type with a String (+), the value of the basic data type will be automatically converted into a String type.
Cast (display)
1. Features: The code requires special format processing and cannot be completed automatically.
2. Rules: Data range from small to large
The format is as follows:
int b = (int)10L;
Integer types: byte, short, int, long
Note: Java's integer constant defaults to int type. When declaring a long constant, you must add 'l' or 'L' after it.
Floating point types: float, double
Note: Java's floating-point constants are double by default. To declare a float constant, add 'f' or 'F' after it.
Character type: char
Note: Operations can be performed on the char type. Because it all corresponds to Unicode code.
Boolean type: boolean
Note: boolean type data only allows values of true and false, not null.
The boolean type cannot be converted to other data types.
(5) Operators
Classification of operators
1. Arithmetic operators
Increment (decrement) operators: ++, –
Example: a++: assignment first and then operation
int a = 1;
int b = a++;
System.out.print(a);//a=2
System.out.print(b);//b=1
Example: ++a: operation first and then assignment
int a = 1;
int b = ++a;
System.out.print(a);//a=2
System.out.print(b);//b=2
2. Assignment operator
3. Comparison operators (relational operators)
Notice:
1.>, <, >=, <= only support that the left and right operands are numeric types
2.==, != The operands on both sides can be either numerical types or reference types
3.== When comparing basic data types, values are compared. When comparing reference data types, addresses are compared.
4. Logical operators
The difference between “&” and “&&”
When there is a single &, no matter whether the left side is true or false, the right side is calculated;
When double & is used, if the left side is true, the right side participates in the operation; if the left side is false, the right side does not participate in the operation.
The difference between "|" and "||" is the same. || means: when the left side is true, the right side does not participate in the operation.
5. Bit operators
Bit operations are operations performed directly on the binary representation of integers.
6.Ternary operator
Syntax form: Boolean expression? Expression 1: Expression 2
The Boolean expression is true, and the operation result is expression 1. Otherwise, the result is expression 2.
int a = (10>3) ? 5 : 10;//10>3为true,a=5
int a = (10<3) ? 5 : 10;//10<3为false,a=10
(5) Operator precedence
(6) Program flow control
The three basic process structures specified in structured programming are: sequential structure, branch structure, and loop structure.
sequential structure
The program is executed from top to bottom.
public class Test{
int num1 = 12;
int num2 = num1 + 2;
}//java中定义成员变量时采用合法的向前引用。
Branch statement if-else
If statement has three formats:
Branch statement switch-case
public class SwitchTest {
public static void main(String args[]) {
int i = 1;
switch (i) {
case 0:
System.out.println("one");
break;
case 1:
System.out.println("two");
break;
default:
System.out.println("other");
break;
}
}
}
Notice:
1. The value of the expression in switch (expression) must be one of the following types: byte, short, char, int, enumeration (jdk 5.0), String (jdk 7.0);
2. The break statement is used to make the program jump out of the switch statement block after executing a case branch; if there is no break, the program will be executed sequentially until the end of the switch.
Loop structure
The loop statement consists of four components
1.Initialization part
2. Loop condition part
3. Loop body part
4. Iteration part
Loop structure: for loop
public class ForLoop {
public static void main(String args[]) {
int result = 0;
for (int i = 1; i <= 100; i++) {
//1.初始化部分 2.循环条件部分 3.迭代部分
result += i;//循环体部分
}
System.out.println("result=" + result);
}
}
Loop structure: while loop
public class WhileLoop {
public static void main(String args[]) {
int result = 0;
int i = 1;//1.初始化部分
while (i <= 100) {
//循环条件
result += i;//循环体部分
i++;//迭代部分
}
System.out.println("result=" + result);
}
}
Loop structure: do-while loop
public class DoWhileLoop {
public static void main(String args[]) {
int result = 0, i = 1;//初始化部分
do {
result += i;//循环体部分
i++;//迭代部分
} while (i <= 100);//循环条件部分
System.out.println("result=" + result);
}
}
Note: The difference between while and do...while
- while: first judge and then execute. If the condition is not true, the loop body will not be executed again.
- do...while: execute first and then determine if the condition is not true. The loop body is executed at least once.
(8) Special keywords break, continue
break statement
The break statement is used to terminate the execution of a statement block
public class BreakTest{
public static void main(String args[]){
for(int i = 0; i<10; i++){
if(i==3)
break;//当条件成立时,终止for循环
System.out.println(" i =" + i);
}
System.out.println("Game Over!");
}
}
continue statement
continue can only be used in loop structures
The continue statement is used to skip one execution of the loop statement block and continue the next loop.
public class ContinueTest {
public static void main(String args[]){
for (int i = 0; i < 100; i++) {
if (i%10==0)
continue;//跳出成立的循环
System.out.println(i);
}
}
}
Notice:
1.return: It is not specifically used to end the loop. Its function is to end a method. When a method executes a return statement, the method will be terminated.
2. Different from break and continue, return directly ends the entire method, no matter how many levels of loops this return is in