Complete use of the basic syntax of java Foundation

1, Keywords and identifiers

1. Use of java keywords

Definition: a string (word) with special meaning given by the Java language for special purposes
Features: all letters in the keyword are lowercase

Specific keywords:

2. Reserved word

The current Java version has not been used, but later versions may be used as keywords.
What are the reserved words: goto and const
Note: avoid using these reserved words when naming your own identifiers

3. Use of identifiers

Definition: any place where you can name yourself is called an identifier.
Structures involved:
Package name, class name, interface name, variable name, method name, constant name
Rule: (must be followed. Otherwise, the compilation fails)

Note:
When naming, in order to improve the reading ability, we should try our best to "see the name and know the meaning".

2, Use of variables (key points)

1. Classification of variables

1.1 classification by data type

detailed description:

//1. Integer: byte(1 byte = 8bit) \ short(2 bytes) \ int(4 bytes) \ long(8 bytes)
		//① byte range: - 128 ~ 127
	// ② Declare a long variable, which must end with 'l' or 'l'
		// ③ In general, int is used when defining integer variables.
       //④ Integer constant. The default type is int
//2. Floating point type: float(4 bytes) \ double(8 bytes)
		//① Floating point type that represents a numeric value with a decimal point
		//② float indicates that the range of values is larger than long
	//③ When defining a float type variable, the variable should end with "F" or "F"
	//④ Usually, double is used when defining floating-point variables.
	//⑤ Floating point constant. The default type is double
//3. Character type: char (1 character = 2 bytes)
		//① A pair of '' is usually used to define char type variables, and only one character can be written inside
	//② Representation: 1 Declare a character 2 Escape character 3 Use Unicode values directly to represent character constants
//4. boolean
	//① You can only take one of two values: true and false
	//② It is often used in condition judgment and loop structure

1.2 classification by declared location (understanding)

2. Defines the format of the variable

Data type variable name = variable value;
or
Data type variable name;
Variable name = variable value;

3. Notes on variable use

① Variables must be declared before use
② Variables are defined within their scope. It is valid within the scope. In other words, out of scope, it is invalid
③ Two variables with the same name cannot be declared in the same scope

4. Operation rules between variables of basic data type

4.1 basic data types involved: 7 types except boolean

4.2 automatic type conversion (only 7 basic data types are involved)

Conclusion: when the variables of the data type with small capacity are calculated with the variables of the data type with large capacity, the result will be automatically promoted to the data type with large capacity.
	byte ,char ,short --> int --> long --> float --> double 
	Special: when byte,char,short When three types of variables are operated, the result is int type
 Note: the capacity at this time refers to the large and small range of numbers. For example: float Capacity should be greater than long Capacity of

4.3 forced type conversion (involving only 7 basic data types): the inverse operation of automatic type promotion operation.

1. Strong conversion character is required: ()
2. Note: forced type conversion may cause precision loss.

4.4 operation between string and 8 basic data types

1. String It belongs to the reference data type,String
2. statement String Type variable, use a pair of""
3. String It can operate with 8 basic data type variables, and the operation can only be connection operation:+
4. The result of the operation is still String type
 Avoid:
String s = 123;//Compilation error
String s1 = "123";
int i = (int)s1;//Compilation error

3, Hex (understand)

1. Hexadecimal and representation involved in programming

2. Instructions for binary

  • 2.1 storage mode at the bottom of the computer: all numbers exist in binary form at the bottom of the computer.
  • 2.2 storage mode of binary data: all values, whether positive or negative, are stored in the form of complement at the bottom.
  • 2.3 description of original code, inverse code and complement code:
    Positive number: three in one

Negative number:

3. Conversion between hexadecimals

3.1 diagram

3.2 diagram binary conversion to decimal



3.3 figure decimal to binary conversion

3.4 conversion between binary, octal and hexadecimal

4, Operator

1. Arithmetic operator

Arithmetic operators: + - + - * /% (front) + + (rear) + + (front) -- (rear) --+

[[typical code]
		//Division:/
		int num1 = 12;
		int num2 = 5;
		int result1 = num1 / num2;
		System.out.println(result1);//2
		// %: remainder operation
		//The sign of the result is the same as that of the module
		//In development, we often use% to judge whether it can be eliminated.
		int m1 = 12;
		int n1 = 5;
		System.out.println("m1 % n1 = " + m1 % n1);

		int m2 = -12;
		int n2 = 5;
		System.out.println("m2 % n2 = " + m2 % n2);

		int m3 = 12;
		int n3 = -5;
		System.out.println("m3 % n3 = " + m3 % n3);

		int m4 = -12;
		int n4 = -5;
		System.out.println("m4 % n4 = " + m4 % n4);
		//(front) + +: increase by 1 before operation
		//(later) + +: calculate first and then increase by 1
		int a1 = 10;
		int b1 = ++a1;
		System.out.println("a1 = " + a1 + ",b1 = " + b1);
		
		int a2 = 10;
		int b2 = a2++;
		System.out.println("a2 = " + a2 + ",b2 = " + b2);
		
		int a3 = 10;
		++a3;//a3++;
		int b3 = a3;
		//(front) --: subtract 1 first and then operate
		//(later) --: operation first, then self subtraction 1
		
		int a4 = 10;
		int b4 = a4--;//int b4 = --a4;
		System.out.println("a4 = " + a4 + ",b4 = " + b4);
[[specially specified]
1.//(front) + +: increase by 1 before operation
 //(later) + +: calculate first and then increase by 1
2.//(front) --: subtract 1 first and then operate
  //(later) --: operation first, then self subtraction 1
3.Connector:+: Can only be used in String Used with other data type variables.

2. Assignment operator

Assignment operator: = + = - = * = / =%=

[typical code]
		int i2,j2;
		//continuous assignment 
		i2 = j2 = 10;
		//***************
		int i3 = 10,j3 = 20;
		int num1 = 10;
		num1 += 2;//num1 = num1 + 2;
		System.out.println(num1);//12

		int num2 = 12;
		num2 %= 5;//num2 = num2 % 5;
		System.out.println(num2);

		short s1 = 10;
		//s1 = s1 + 2;// Compilation failed
		s1 += 2;//Conclusion: the data type of the variable itself will not be changed
		System.out.println(s1);

[[specially specified]
1.The result of the operation does not change the data type of the variable itself
2.
		//In development, if you want the variable to realize the operation of + 2, how many methods are there? (premise: int num = 10;)
		//Method 1: num = num + 2;
		//Mode 2: num += 2; (recommended)
		
		//In development, if you want the variable to realize the operation of + 1, how many methods are there? (premise: int num = 10;)
		//Method 1: num = num + 1;
		//Mode 2: num += 1; 
		//Mode 3: num + +; (recommended)

3. Comparison operator

Comparison operator (relational operator): = =! = > < > =<= instanceof

[[typical code]
		int i = 10;
		int j = 20;

		System.out.println(i == j);//false
		System.out.println(i = j);//20

		boolean b1 = true;
		boolean b2 = false;
		System.out.println(b2 == b1);//false
		System.out.println(b2 = b1);//true
[[specially specified]
1.The result of the comparison operator is boolean type
2.>  <  >=  <= :Can only be used between numeric data types.
3. == and !=: It can be used not only between numeric type data, but also between other reference type variables.
Account acct1 = new Account(1000);
Account acct2 = new Account(1000);
boolean b1 = (acct1 == acct2);//Compare whether two accounts are the same Account.
boolean b2 = (acct1 != acct2);//

4. Logical operator

Logical operators: & & | |^

[[typical code]
		//Distinguish & and&&
		//Same point 1: & and & & have the same operation result
		//Same point 2: when the left side of the symbol is true, both will perform the operation on the right side of the symbol
		//Difference: when the left side of the symbol is false, & continue the operation on the right side of the symbol&& The operation to the right of the symbol is no longer performed.
		//In development, recommended&&
		boolean b1 = true;
		b1 = false;
		int num1 = 10;
		if(b1 & (num1++ > 0)){
			System.out.println("I'm in Beijing now");
		}else{
			System.out.println("I'm in Nanjing now");
		}

		System.out.println("num1 = " + num1);
		

		boolean b2 = true;
		b2 = false;
		int num2 = 10;
		if(b2 && (num2++ > 0)){
			System.out.println("I'm in Beijing now");
		}else{
			System.out.println("I'm in Nanjing now");
		}

		System.out.println("num2 = " + num2);

		// Distinction: | and|| 
		//Same point 1: the operation results of | and | are the same
		//Same point 2: when the left side of the symbol is false, both will perform the operation on the right side of the symbol
		//Difference 3: when the left side of the symbol is true, | continue to perform the operation on the right side of the symbol, and | no longer perform the operation on the right side of the symbol
		//In development, recommended||
		boolean b3 = false;
		b3 = true;
		int num3 = 10;
		if(b3 | (num3++ > 0)){
			System.out.println("I'm in Beijing now");
		}else{
			System.out.println("I'm in Nanjing now");
		}
		System.out.println("num3 = " + num3);


		boolean b4 = false;
		b4 = true;
		int num4 = 10;
		if(b4 || (num4++ > 0)){
			System.out.println("I'm in Beijing now");
		}else{
			System.out.println("I'm in Nanjing now");
		}
		System.out.println("num4 = " + num4);
[[specially specified]
1.Logical operators operate on boolean Variable of type. And the result is the same boolean type

5. Bitwise operator

Bitwise operators: < > > > & ^~

[[typical code]
		int i = 21;
		i = -21;
		System.out.println("i << 2 :" + (i << 2));
		System.out.println("i << 3 :" + (i << 3));
		System.out.println("i << 27 :" + (i << 27));

		int m = 12;
		int n = 5;
		System.out.println("m & n :" + (m & n));
		System.out.println("m | n :" + (m | n));
		System.out.println("m ^ n :" + (m ^ n));
[Can you write the most efficient 2 * 8 How to implement? 
Answer: 2 << 3  Or 8 << 1
[[specially specified]
1. Bitwise operators operate on integer data
2. << : Within a certain range, every 1 bit to the left is equivalent to * 2
   >> :Within a certain range, each shift to the right by 1 bit is equivalent to / 2

Typical topic:
1.Exchange the values of two variables.
2.Binary to hexadecimal conversion of 60

6. Ternary operator

Ternary operator: (conditional expression)? Expression 1: expression 2

[[typical code]
1.Gets the larger value of two integers
2.Gets the maximum of three numbers
[[specially specified]
1. explain
① The result of the conditional expression is boolean type
② Whether to execute expression 1 or expression 2 depends on whether the conditional expression is true or false.
  If the expression is true,Expression 1 is executed.
  If the expression is false,Expression 2 is executed.
③ Expression 1 and expression 2 are required to be consistent.
④ Ternary operators can be nested
2. 
Where ternary operators can be used, they can be rewritten as if-else
 On the contrary, it does not hold.
3. If the program can use both ternary operators and if-else Structure, the ternary operator is preferred. Reason: simplicity and high execution efficiency.

5, Process control

1. Process control

Sequential structure: the program is executed from top to bottom.

Branch structure:
if-else if - else
switch-case

Cycle structure:
for
while
do-while

2. Branching structure

1. If else conditional judgment structure

Structure I:

if(Conditional expression){
	Execute expression
}

Structure 2: one out of two

if(Conditional expression){
	Execute expression 1
}else{
	Execute expression 2
}

Structure 3: one out of n

if(Conditional expression){
	Execute expression 1
}else if(Conditional expression){
	Execute expression 2
}else if(Conditional expression){
	Execute expression 3
}
...
else{
	Execute expression n
}

2. Explain

1. else Structure is optional.
2. For conditional expressions:
   > If multiple conditional expressions are mutually exclusive(Or no intersection relationship),It doesn't matter which judgment and execution statement is declared above or below.
   > If there is an intersection relationship between multiple conditional expressions, you need to consider which structure should be declared on it according to the actual situation.
   > If there is an inclusive relationship between multiple conditional expressions, it is usually necessary to declare a small range on a large range. Otherwise, those with small scope will have no chance to implement.
3. if-else Structures can be nested.
4. If if-else When the execution statement in the structure has only one line, the corresponding pair{}Can be omitted. However, it is not recommended to omit.

3. Switch case selection structure

switch(expression){
case Constant 1:
	Execute statement 1;
	//break;
case Constant 2:
	Execute statement 2;
	//break;
...
default:
	Execute statement n;
	//break;
}

3.1 description

① according to switch The values in the expression match each in turn case Constant in. Once the match is successful, enter the corresponding case In the structure, its execution statement is called.
  When the execution statement is called, it still continues to execute other statements downward case Structure until encountered break Keyword or this switch-case structure
  At the end.
② break,Can be used in switch-case Structure, which means that once this keyword is executed, it will jump out switch-case structure
③ switch The expression in the structure can only be one of the following six data types:
   byte ,short,char,int,Enumeration type(JDK5.0 newly added),String type(JDK7.0 newly added)
④ case Only constants can be declared later. Scope cannot be declared.
⑤ break Keywords are optional.
⑥ default:amount to if-else In structure else.  
  default The structure is optional and the location is flexible.

3.2. If the execution statements of multiple cases in the switch case structure are the same, consolidation can be considered.

3.3. break is optional in switch case

4. Cyclic structure

5. Four elements of circular structure

① Initialization condition
② Loop condition - > is of boolean type
③ Circulatory body
④ Iterative condition
Note: generally, the loop ends because the loop condition in ② returns false.

6. Three cycle structures

6.1 for loop structure

for Cyclic structure
for(①;②;④){
	③
}
Execution process:① - ② - ③ - ④ - ② - ③ - ④ - ... - ②

6.2 while loop structure

①
while(②){
	③;
	④;
}
Execution process:① - ② - ③ - ④ - ② - ③ - ④ - ... - ②
explain:
write while Loop be careful not to lose the iteration condition. Once lost, it may lead to an endless cycle!

6.3 summary of for and while cycles:

  1. During development, we will basically choose from for and while to implement the loop structure.
  2. for loop and while loop can be converted to each other!
    Difference: the scope of the initialization condition part of the for loop and the while loop is different.
  3. We write programs to avoid dead cycles.

6.4 do while loop structure

①
do{
	③;
	④;
}while(②);
Execution process:① - ③ - ④ - ② - ③ - ④ - ... - ②

explain:
1.do-while The loop will execute the loop body at least once!
2.In development, use for and while More. Less use do-while

7. "Infinite loop" structure: while(true) or for(;)

Summary: how to end a loop structure?
Method 1: when the loop condition is false
Method 2: execute break in the loop body

8. Nested loop

8.1 nested loop

1.Nested loop:A loop structure A Declared in another loop structure B In the circulating body of,This constitutes a nested loop
  Inner loop: loop structure A
  Outer loop: loop structure B

8.2. explain

① The inner loop structure is traversed once, which is only equivalent to the execution of the outer loop body once
② Suppose that the outer loop needs to be executed m times and the inner loop needs to be executed n times. At this time, the loop body of the inner loop has been executed m * n times
③ The outer loop controls the number of rows and the inner loop controls the number of columns

[[typical exercise]
		//Exercise 1:
		/*
		******
		******
		******
		******
		*/
		for(int j = 1;j <= 4;j++ ){
			for(int i = 1;i <= 6;i++){
				System.out.print('*');
			}
			System.out.println();
		}
		//Exercise 2:
		/*			i((line number) 		 j(*)
		*			1			1
		**			2			2
		***			3			3
		****		4			4
		*****		5			5
		*/

		for(int i = 1;i <= 5;i++){//Number of control rows
			for(int j = 1;j <= i;j++){//Number of control columns
				System.out.print("*");
			
			}
			System.out.println();
		}
		//Exercise 3: 99 multiplication table
		//Exercise 4: prime numbers within 100

Supplement: measure the advantages and disadvantages of a function code:
1. Correctness
2. Readability
3. Robustness

8.3. High efficiency and low storage

Time complexity and space complexity (measure the quality of the algorithm)

Keywords: Java Back-end

Added by biopv on Sun, 23 Jan 2022 11:24:18 +0200