运算符
Arithmetic operators: +,-,*,/,%,++,-- Assignment operators Relational operators Logical operators Ternary operators
++, -- operator
++,--operator: +1/-1 operation for variable Algorithm : 1. The value of the variable is +1/-1 operation2 . The changed value is reassigned to the original variable Notes : 1. + +,-- can only be used with variables, not with constants For example: a++, b-- can't -> 10++ 2. ++, -- can be placed in front of the variable or behind the variable a. When used alone: ++,-- put before and after the variable has no effect on the operation result b. When participating in other operations: ++,-- in front of the variable, first increase or decrease before participating in the operation ++,-- After the variable, first participate in the operation and then self-increment or self-decrement ++ ,-- the case of using it alone is the most, and rarely use the self-increment/self-decrement rule that participates in the operation!
assignment operator
= : Assign the value on the right of the = sign to the variable on the left For example: int a = 10; -> Assign 10 to the variable a of type int Extended assignment operator: +=,-=,*=,/=,% = -> The extended assignment operator implies a coercion expansion : a += value; ==> a = (datatype of a)(a + value);
relational operator
Relational operators: ==,!=,>,>=,<,<= Notes: 1. The formula connected by relational operators is called a relational expression! 2. The result of the relational expression must be a boolean type3 . Do not write == as =
Logical Operators
Logical operator: Classification of connection relational expressions: &: logical AND -> and if false is false |: logical or -> or true if true !: logical not -> Negative true is false, false is true ^: Logical XOR -> China's marriage certificate relationship If the same is false, the different is true 0 < a < 10 -> a > 0 & a < 10; a < 0 or a > 10 -> a < 0 | a > 10; Extended logical operators: &&: false if false -> short-circuit effect: the left is false, and the expression on the right is not executed! || : true if true -> short-circuit effect: true on the left , the expression on the right is not executed! Note : 1. The result of the logical expression must also be of boolean type 2. As long as logical AND and logical OR are used, please use logical double AND and double OR
Binary logical operations and shift operations
//Binary logical operation: 0 is false, 1 is true System.out.println(3 & 5);//1 00000000 00000000 00000000 00000011 --> 3 & : false is false 00000000 00000000 00000000 000 00101 -- > 5 ------------------------------------------------ 00000000 00000000 00000000 00000001 --> 1 System.out.println(3 | 5);//7 00000000 00000000 00000000 00000011 --> 3 | : true if true 00000000 00000 000 00000000 00000101 --> 5 ----- ---------------------------------------------- 00000000 00000000 00000000 00000111 --> 7 System.out.println(3 ^ 5);//6 00000000 00000000 00000000 00000011 --> 3 ^ : false if same, true if different 00000000 00000000 00000000 00000101 --> 5 --------- --------------------------------------- 00000000 00000000 00000000 00000110 --> 6 in binary Shift operation: right shift>>, left shift<< //binary shift operation //right shift>> must be smaller System.out.println(8 >> 1);//4 00000000 00000000 00000000 00001000 --> 8 000000000 00000000 00000000 0000100|0 -> Right shift: Move a few bits to the right The result is: 4 //Left shift<< must be bigger System.out.println(8 << 1);//16 00000000 00000000 00000000 00001000 --> 8 0|0000000 00000000 00000000 000010000 --> Left shift: Move a few bits to the left The structure is: 16 1.
Ternary Operator (Mastering)
Element: block, element Unary operator: ++,--,! Binary operator: Most operators are binary operators Ternary operator: format: the data type of the result variable name = the result is boolean class The formula? Result 1 : Result 2; Execution process: 1. The calculation result is the result of the boolean type formula 2. If the result is true, the result of the entire ternary is result 1 3. If the result is false, the result of the entire ternary The result is the problem solved result 2 : find the larger value of 2 numbers, find the larger value of 3 numbers
Keyboard entry function (master)
Keyboard entry function: the class of Scanner keyboard entry function -> objects of this class can complete the keyboard entry function. Steps to use: 1. Import package: import this class from JDK into your own code import java.util.Scanner; // Write on the top of the class, below the package //use IDEA to develop, IDEA automatically imports the package!! 2. Create an object: Scanner object name = new Scanner(System.in);//object name: the programmer can name it himself- > sc 2.5 : Prompt the user to act 3. Receive the entered integer: int variable name = object name.nextInt(); //variable name: can be named by the programmer //define an int type variable to receive the entered one Integer Notes : 1. Keyboard input requires user cooperation 2. Input integers can only receive integers InputMismatchException: input mismatch exception 3. If you want to enter multiple times, you only need to use the object call method multiple times, and you don’t need to create objects repeatedly -> Objects can be reused
process control
1. Sequence structure 2. Branch structure switch if..else if..else 3. Loop structure for while do..while
sequential structure
Sequence structure: enter from the main method, execute code from top to bottom, from left to right (parentheses have the highest priority) from left to right: from left to right on the right side of the equal sign int sum = a + b;
branch structure
Branch structure: There is a fork in the code, you need to make a choice, which way to go; the branch structure you have learned: ternary operator After the branch structure is executed, the code behind the branch structure will be executed
switch statement (understanding)
switch : branch, switch Format : switch(expression){ case value 1: statement body 1; break; case value 2: statement body 2; break; case value 3: statement body 3; break; … . default : statement body n+1; break; } Format explanation: switch : represents the switch branch structure Expression: only byte, short, char, int, String, variable/constant value of one of the above types of enumeration objects can be filled //In most cases, this expression is filled with variables!! //If you remember it yourself: integer int and string String case: event, situation -> how many cases there are means how many situations there are (branches) value: must be a constant value, and the constant value must be consistent with the type of the expression! Statement body: the code that needs to be executed when the result of the expression matches the value after the case //This statement body can be multiple sentences of code break: break ,end->end switch statement default: default, when the values after all cases do not match the expression result, if there is a default statement, execute the statement body n+1 in the default statement; //Back to the bottom Execution process: 1. Calculate the result of the expression in the parentheses of the switch 2. Match it with the value behind the case from top to bottom -> Match: do an equal (expression result == value) comparison 3. If the result is true, enter the current case and execute the statement body in the case, and end the switch statement immediately when encountering a break 4. If all the cases do not match the result of the expression, if there is a default statement, execute the statement body n+1 in the default statement ; case penetration phenomenon: When the execution of the statement body in the case does not encounter a break, then there will be a breakdown effect, and the statement body in the case below will be executed directly until the backbracket of the break or switch is encountered to end the switch statement// Disadvantages Bigger than profit: remember to write break when writing switch!!
if statement (emphasis)
if : if statement -> If, suppose the first format of if if (result is an expression of boolean type) { statement body; } //if code behind execution process: 1. Calculate the expression in parentheses if 2. If the result is true, execute the statement body inside the if braces; 3. If the result is false, end the if statement and execute other codes behind the if structure. The first format of if is a judgment operation for a situation ! Pseudo-code: if(whether Xiaoli is coming){ //Only when Xiaoli comes can the code in the curly brackets be executed to go to the grove; } The second format of if if(the result is a boolean expression formula) { statement body 1; }else{ statement body 2; } //if behind the code Execution process: 1. Calculate the result of the expression in the if parentheses 3. If the result is false, execute the statement body in the else curly brackets 2; 2. If the result is true, execute the statement body in the if braces 1; 4. Execute the code behind the if The second format of if Pseudocode: if(Xiaoli is coming or not ){ //The code that can be executed when Xiaoli comes to the grove; }else{ //The code that can be executed when Xiaoli is not coming is about Xiaoli's best friend Xiaohong; } The third format of if if (the result is an expression of boolean type 1) { statement body 1; } else if (the result is an expression of boolean type 2) { statement body 2; }... There can be many elses here if else{ statement body n+1; } Execution process: 1. Calculate the result of expression 1, if it is true, execute statement body 1; 2. If the result of expression 1 is false, calculate the result of expression 2, if Execute statement 2 for true; 3. If the result of expression 2 is false, continue to calculate other expressions in the if parentheses below ...... 4. If all If the judgment results are all false, if there is an else structure, execute the statement body n+1 in the else structure The third format of if is a judgment operation for three or more situations! Pseudo-code: if(Xiaoli will come or not){ //The code that can be executed when Xiaoli comes to Xiaoli Walk in the woods; }else if(Xiaoli's boyfriend is here?){ Let's play LOL together; }else if(Xiaoli's girlfriend, Xiaohong, is here with her girlfriend?){ Let's go for a walk in the woods; // _ _ _ _ _ At this stage, you must write curly braces when writing the if structure!!
loop structure
Loop structure: improve the reusability of code The composition of the loop 1. Initialization statement: define a variable to control the loop -> define the variable once!! 2. Conditional judgment statement: the essence is the formula of the boolean type result- > Decide whether to execute repeated things!! 3. Control conditional statement (step expression): Change the initialization statement, and the direction of change must go in the direction that does not satisfy the loop condition 4. Loop body statement: Repeatedly executed code
for loop structure (emphasis)
For loop structure format: for (initialization statement 1; conditional judgment statement 2; step expression 3) { loop body statement 4; } Execution process: 1. Execute the initialization statement 2. Execute the judgment conditional statement -> false: end immediately Loop 3. If the result of the judgment condition statement is true, execute the loop body statement 4. Execute the step expression 5 and repeat the execution from the second step Execution process code: 1 -> 2 -true-> 4 -> 3 -> 2 - true-> 4 -> 3 -> .... -false-> end loop -false-> end loop shortcut code: number of times.fori: number of positive sequence loops.forr: reverse order loop The variable defined by the initialization statement in the for
Debug breakpoint debugging (emphasis)
Breakpoint debugging code: The principle of debugging slows down the code, and executes the code step by step according to the programmer's operation. Breakpoint debugging steps: 1. Breakpoint: click on the line number (click again to cancel) where to hit ? Where will not be clicked (click a breakpoint in a method, click on the executable code) 2. Breakpoint execution -> debug