The basic syntax of JavaSE includes bit operator, operator priority, if statement, switch statement and the use of for loop

8. Bitwise operator

& | ^

Convert the operand to binary, and then operate according to the rules of the operator, and the final result is converted to decimal

  • &: two numbers in the same bit. If both are 1, it is 1, and if one is 0, it is 0
  • |: two numbers in the same bit, one is 1, and both are 0
  • ^: two numbers in the same bit, the same is 0, and the different is 1

Note: the above three left and right sides are boolean type data, which exists as logical operators

  • Shift Bitwise Operators

    >> <<

    • >>Shift right
      • Convert the first operand to binary, move the second operand to the right by one digit, directly ignore the removed digits, and convert the rest to decimal
    • < < move left
      • Convert the first operand to binary, move the second operand to the left by one digit, fill the empty digit with 0, and the result is converted to decimal
  • Summary law

    • Shift right: / 2 power
      • 8>>3 ==> 8/2^3
    • Shift left: * 2 power
      • 8<<3 ==> 8*2^3
  • be careful:

    • It is suggested to use displacement operator instead of multiplication and division, which is efficient
9. Operator priority

Pithy formula: single eye numeral relation, logical three eye post assignment

public class Class020_Operator{
	//main method  
	public static void main(String[] args){
		int a = 5;
		int b = 10;
		boolean x = a < b ? a++ * 2 > b || a > b : a < b;
		System.out.println(x);  //F
		System.out.println(a);  //6
		System.out.println(b);  //10
		//&&Higher priority than
		System.out.println(false && true || true);  //T
		System.out.println(true || true && false);  //T
		
		//Assignment operator: execute from right to left
		//x=y=z=10;
		
		//Conditional operators are right to left associative
		boolean w = true?true:false?false:false;  // 	T
		boolean y = true?true:(false?false:false);  //Combine T from right to left
		boolean z = (true?true:false)?false:false;  //Combine F from left to right
		System.out.println(w); 
		System.out.println(y); 
		System.out.println(z); 
        
	}
}

2.3.8 process control statement

1. Use of if statement
  • Single selection | single branch:
    if(boolean expression){
    Sentence body;
    }

    • Execution process:
      1.boolean expression to get the boolean result
      2. If the result is true, execute the statement body in {}
      3. If the result is false, skip the if structure directly
  • Double selection | double branch:
    if(boolean expression){
    Sentence body 1;
    }else{
    Sentence body 2;
    }

    • Execution process:
      1.boolean expression to get the boolean result
      2. If the result is true, execute the statement body 1 in {}
      3. If the result is false, the statement body after else is executed 2
  • Multiple selection | multiple branches:
    if(boolean expression 1){
    Sentence body 1;
    }else if(boolean expression 2){
    Sentence body 2;
    }else if(boolean expression 3){
    Sentence body 3;
    }...
    else{
    Statement body n;
    }

    • Execution process:
      1.boolean expression 1 obtains the boolean result. If the result is true, execute statement body 1 in {}
      2. If the result is false, execute boolean expression 2. If the result is true, execute statement body 2 in {}
      3. If the result is false, execute boolean expression 3. If the result is true, execute statement body 3 in {}
      ...
      4. If none of the above is satisfied, the statement body n after else is executed;
  • be careful:

    • An if... else is a structure and can only execute one statement body

    • If there is only one sentence in {}, the {} before and after can be omitted

      public class Class021_If{
      	
      	public static void main(String[] args){
      		System.out.println("if before");
      		//Shan Xuanze
      		if(false){
      			System.out.println("Meet the conditions");
      		}
      		System.out.println("if after");
      		
      		//Shuangxuanze
      		int i=1;
      		if(i>0){
      			System.out.println("i>0");
      		}else{
      			if(i==0){
      				System.out.println("i=0");
      			}else{
      				System.out.println("i<0");
      			}
      		}
      		
      		//Duoxuanze
      		if(i>0) 
      			System.out.println("i>0");
      		else if(i==0) 
      			System.out.println("i=0");
      		else 
      			System.out.println("i<0");	
      		
      	}
      	
      }
      
2. Use of switch statement
  • Syntax:
    Switch (condition){
    case value 1:
    Sentence body 1;
    break;
    case value 2:
    Sentence body 2;
    break;
    ...
    default:
    Statement body n;
    break;
    }

  • Conditions: variables, expressions

    Enumeration type: jd.short (k1.string)

    • Case: case follows the fixed value, which should be judged with the result of the condition. If the result of the condition is equal to the fixed value after case, the corresponding statement body will be specified
    • break: end the current switch statement to prevent case penetration
    • default: equivalent to else. It can be defined or not. It can be defined anywhere in the switch statement
public class Class023_Switch{
	public static void main(String[] args){
		String flag = "Purple lamp";
		
		switch(flag){
			case "green light":
				System.out.println("pass at a green light..");
				break;
			case "red light":
				System.out.println("Stop at red light..");
				break;
			case "Yellow light":
				System.out.println("Yellow light, wait a minute..");
				break;	
			default :  
				System.out.println("Don't make trouble..");
				break;	
		}
		/*
		
		//case pierce through
		switch(flag){
			case "Green light ":
				System.out.println("pass at a green light.. ");
			case "Red light ":
				System.out.println("Stop at a red light ");
			case "Yellow light ":
				System.out.println("Yellow light, wait ");
				break;
			default :  
				System.out.println("Don't make trouble ");	
		}
		*/
		
		//Test char
		char ch = 'a';
		switch(ch){
			case 'a':
				System.out.println("aaa..");
				break;	
			case 'b':
				System.out.println("bbb..");
				break;				
		}
		
		//reflection: 
		boolean  b = false;
		//Judge b by switch statement
		int i = b?1:0;
		switch(i){
			case 0:
				System.out.println("false..");
				break;
			case 1:
				System.out.println("true..");
				break;
		}
		
		switch(b+""){
			case "false":
				System.out.println("false..");
				break;
			case "true":
				System.out.println("true..");
				break;
		}
		
		//Seasonal judgment uses the penetration of break
		int month = 3;
		switch(month){
			case 3:
			case 4:
			case 5:
				System.out.println("spring..");
				break;
			case 6:
			case 7:
			case 8:
				System.out.println("summer..");
				break;
			case 12:
			case 1:
			case 2:
				System.out.println("winter..");
				break;
		}
				
	}
}
3.for loop
  • Syntax:

    for loop
    For (condition initialization; condition judgment; condition change){
    Repeatedly executed code segments;
    }

    • Conditional initialization: declare a variable i and assign it for the first time
    • Condition judgment: judgment result with boolean value
      End of control cycle
    • Condition change: change of i's own value
  • Execution process:

    • Condition initialization i=1
    • Condition judgment: judge whether the result is false, the loop ends, and true. Execute the loop body sentence I < 5
    • Condition change i++
    • Repeat steps 2 and 3
  • be careful:

    • Conditional variable i can be used in circular sentences
    • The conditional variable i in the for loop is scoped only in the current loop
    • If the loop statement has only one sentence, the {} before and after can be omitted
    public class Class024_For{
    	public static void main(String[] args){
    		
    		//If you execute a piece of code repeatedly, you can consider whether you can use a loop
    		for(int i=1;i<=100;i++){
    			System.out.println("Extreme employment,Challenge the limits!!!!"+i);
    		}
    		
    		//Print integers between 1 and 10
    		//1) Repeat output and printing 2) the output content is regular, determine the change interval [1,10], and determine the change law + 1 each time
    		for(int i=1;i<=10;i++ ){
    			System.out.println(i);
    		}
    		
    		//System.out.println(i);    Error: symbol not found
    		
    		//Find the sum of integers between 1 and 10
    		//1. Repeated summation 2 For integers between 1 and 10 
    		int sum = 0; //Record and 
    		for(int i=1;i<=10;i++){
    			//sum = sum+i;
    			sum+=i;
    		}
    		System.out.println(sum);
    		
    		//Integer sum between 15 and 20
    		//Repeated summation [15,20]
    		sum = 0; 
    		for(int i=15;i<=20;i++)
    			sum+=i;
    		System.out.println(sum);
    		
    	}
    }
    

Keywords: Java Back-end

Added by laura_richer on Sat, 05 Mar 2022 02:50:56 +0200