*** Operator, process control, method

Posted by shaunwor on Tue, 04 Jan 2022 10:54:25 +0100

  1. Operator (continued from 12.29)
    1.1 arithmetic operators
    1.2 relational operators
    1.3 bit operator
    1.4 assignment operator
    =: assign the right to the left, a=2, a=b
    +=: add left and right, assign value to left a+=b, equivalent a=a+b
    -=: left minus right, assigned to the left a-=b, equivalent to a=a-b
    *=
    /=
    %=
    The assignment operator is a right combination, calculated from right to left
    Initialization or left to right
    1.5 string splices
    +Spell multiple data into a string
    If both sides of + are numbers, it is an addition operation
    If there is a string on both sides of + the number is spliced with the string
public static void main(String[] args) {
		String string = "Ah, really loud!@#$int a = 20 Assad 115 \ "'";
		int a = 100;
		int b = 120;
		System.out.println(a+b);
		// a+b=1012
		System.out.println("a+b="+a+b);
		// a+b=22
		System.out.println("a+b="+(a+b));
		// 100+120=220
		System.out.println(a+"+"+b+"="+(a+b));
	}

1.6 ternary operator (ternary operator)
Syntax: Boolean expression? True: false;
True if Boolean is true, false otherwise

public class Operator_06 {

	public static void main(String[] args) {
		int i = 2 < 2 ? 1 : 0;
		System.out.println(i);
		System.out.println(false ? 123 : "It's all fake");
	}
  1. Process control
    Different branches of control program
    2.1 sequence structure
    From top to bottom, from left to right
    2.2 branch structure
    Selectively execute different branches through specified judgment conditions
    2.2.1 if...else...
    • Single branch: non execution
  •  	if(Boolean expression ){
    
  •  			Code executed when true
    
  •  	}
    
  • Double branch: there must be one branch to execute
  •  	if(Boolean expression ){
    
  •  			Code executed when true
    
  •  	}else{
    
  •  			When it's fake,Executed code
    
  •  	}
    
  • Multi branch:
  •  	if(Boolean expression ){}  else if(Boolean expression ){} else if(Boolean expression ){} .....
    
  •  	If multiple branches end with else ending,There must be a branch
    
  •  	If so else if At the end, the branches are not executed
    
  •  	Multi branch case,If one branch is executed, the whole multi branch ends
    
  • If there is only one line of code under each branch, {} can be omitted
public class If_01 {
	public static void main(String[] args) {
		int a = 110;
		int b = 11;
		if (a > 10) {
			System.out.println("1111");
		} else if (a > 50) {
			System.out.println("2222");
		} else if (a > 100) {
			System.out.println("33333");
		}
		
		// Only one line of code braces can be omitted
		if (true) 
			System.out.println("Execution complete");
		else
			System.out.println("Execution complete");
		
		System.out.println("Execution complete");
	}

2.2.2 Switch
* switch : java1. Before 7, you can only pass in int values, but you can pass in strings from 1.7
*

  • Syntax: switch (value) {case value 1: execute statement; break; case value 2: execute statement; break; default: execute statement;
  • break; }
  • Each branch needs to add a break to end the branch, otherwise case penetration will occur
public static void main(String[] args) {
		char c = 'B';
		if (c == 'A') {
			System.out.println("excellent");
		} else if (c == 'B') {
			System.out.println("good");
		} else if (c == 'C') {
			System.out.println("commonly");
		} else {
			System.out.println("difference");
		}
		// The upper and lower programs mean the same thing, but if can judge equality or range, while switch can only judge equality, not range
		switch (c) {
		case 'A':
			System.out.println("excellent");
			break;
		case 'B':
			System.out.println("good");
			break;
		case 'C':
			System.out.println("commonly");
			break;
		default:
			System.out.println("difference");
			break;
		}
		// If the branch does not break, it will lead to case penetration
		int i = 10;
		switch (i) {
		case 1:
			System.out.println(11111);
		case 2:
			System.out.println(22222);
		case 10:
			// Start from the place where the conditions are met, and all subsequent branches will be executed
			System.out.println(101010);
		case 100:
			System.out.println(100100100);
		default:
			System.out.println("Default statement");
		}
	}
public static void main(String[] args) {
		char c = 'C';
		if (c == 'A' || c == 'B') {
			System.out.println("excellent");
		} else if (c == 'C') {
			System.out.println("good");
		}

		// Case merging using case penetration can complete the merging and achieve the effect of 𞓜
		switch (c) {
		case 'A':
		case 'B':
			System.out.println("excellent");
			break;
		case 'C':
			System.out.println("good");
			break;
		}
	}

2.3 circulation structure
2.3.1 For
2.3. 1.1 single cycle

  • The for loop is a counting loop that is executed repeatedly within a certain number of times
  • Syntax structure:
  •  for( Expression 1 ; Expression 2 ; Expression 3 ){
    
  •  	Circulatory body,Is the code that needs to be executed repeatedly
    
  •  }
    
  • Three elements of cycle: start value, end condition and step size
  • Expression 1: it is executed first and only once, so it is suitable for initialization
  • Expression 2: it must be Boolean and determines whether the loop ends, so it is a termination condition
  • Expression 3: it is also executed every cycle, so it can be used as a step
  • Execution process:
  •  Expression 1 is executed first and only once,Then execute expression 2 , If expression 2 is true(Expression 2 must be Boolean) Then the loop body is executed
    
  •  Then execute expression 3 , Then execute expression 2 again ,If it is true Then the loop body is executed , 
    
  •  Then execute expression 3 ,Execute expression 2 again until expression 2 is false Cycle termination
    
// for ( ; ; ) {
		//
		// }
		// The variable i in the loop can only be used in recycling
		for (int i = 0; i < 5; i++) {
			System.out.println(i);
		}
		// System.out.println(i);
		int i;
		for ( i =0 ; i < args.length; i++) {
			System.out.println(i);
		}
		System.out.println(i);

2.3. 1.2 nested loops

  • Nested loop two-layer nesting can simulate two-dimensional tables, and three-layer nesting can simulate three-dimensional bodies
  • You can think of the outer loop as the number of rows and the inner loop as the number of columns
		for (int i = 0; i < 5; i++) {
			for (int j = 0; j < 5; j++) {
				// println will wrap and print will not wrap
				System.out.print(0+"  ");
			}
			// Line feed
			System.out.println();
		}
	

2.3.2 While

  • while: true false loop. If the condition is true, it will be executed; if false, it will be terminated
  • Syntax:
  •  while(Boolean){
    
  •  	Circulatory body;
    
  •  }
    
		int i = 1000;
		while (i<100) {
			System.out.println(i);
			i++;
		}
	

2.3.3 Do...while

  • Do while ensures that the loop executes at least once
  • for and while are executed 0~N times, while dowile is executed 1~N times
		int i = 1000;
		do {
			System.out.println(i);
			i++;
		} while (i < 100);
	

2.3.4 Break&continue

  • Break: break
  • 1 can be used in switch to end the case branch and prevent case penetration
  • 2 can also be used in a loop to end the current loop
		for (int i = 0; i < 10; i++) {
			if (i == 3) {
				// Terminate cycle
				break;
			}
			System.out.println(i);
		}
	
		// Set name for external loop
		outerFor: for (int i = 0; i < 5; i++) {
			for (int j = 0; j < 5; j++) {
				if (j == 3) {
					// When a nested loop occurs, break can only terminate the inner loop
					// break;
					// Terminate the specified loop. In this way, the outer loop can be terminated in the inner loop
					break outerFor;
				}
				System.out.print(j + "  ");
			}
			System.out.println();
		}
	
  • Continue: skip the current cycle and continue next time
		for (int i = 0; i < 10; i++) {
			if (i == 3) {
				// break;
				continue;
			}
			System.out.println(i);
		}
	
  1. method
    3.1 general
  • A method is a collection of many statements. Put this code into a method, and then you can use this method multiple times
  • Method objective: to reuse the code, make the program shorter and clearer, and improve the development efficiency
    3.2 method statement
  • Statement:
  •  Modifier list return value type method name  (parameter list){ Method body }
    
  •  Modifier list :  Can have,Can not,There can be more than one
    
  •  		Permission modification : public  protected private  Don't write one out of four
    
  •  		Other modifications : static , synchronized 
     				abstract , final either-or .....
     				
     return type : 11 Any of the data types , If no return value is required,Then write void
     
     Method name : Method name,Look at the text and know the meaning,Hump nomenclature
     
     parameter list : One thing to do,Required prerequisites,Can be used as input parameters
     		There can be more than one,use , Comma separated, e.g int a , int b , int c....
     		
     		Formal parameter : At the time of method declaration,Defined parameter list
     		Argument : Method call,Actual incoming data
     		
     Method body : Code to execute
     	be careful : return Terminate method run and return
     		If there is a method with return value type,Method body must have return sentence
     				such as public static int m1(){
     					return 1;// The 1 here is only an int value, because the return value is of type int
     				}
     		If there is no return value, you can write return You can also not write ,Even write return Data cannot be returned,Operation can only be terminated
     			public static void m1(){
     				return; // Only the function of terminating method operation
     			}
    

3.3 method classification

  • Method classification
  •  1 Static method : use static Modification method,Is a static method
    
  •  2 Member method : No, static The modification method is the member method
    
  •  3 Construction method : Creating objects using,Leave it alone
    
  • Call:
  •  Static method : Class name.Static method name(parameter) , The class name in the same class can be omitted
    
  •  Member method : object reference.Member's legal name(parameter)
    
  • Method does not call or execute, the call executes, and returns the result to the caller
  • The writing method only considers the implementation of the function. What the method is used to do in the end has nothing to do with the declaration
public class Method_01 {
	public static void main(String[] args) {
		int result = Method_01.m1(123,5312);
		System.out.println(result);
	}
	public static int m1(int a , int b){
		return a+b;
	}
	public static void m2(){
		return ;
	}

Topics: Java Back-end