java learning summary: 3

Keywords: Java C

Logical operation

1. '!' (non)
2. Meet with (multiple conditions)

In Java, both & & and & are logical operators that represent and, and both represent logical transporter and. When the expressions on both sides are true, the whole operation result is true, otherwise it is false.
’&&When the value of the first expression is false, the second expression will not be calculated;
’&'then both expressions execute.

public class Test2 {
        public static void main(String args[]) {
            //If ((1 = = 2) & (10 / 0) = = 0) {/ / (10 / 0) = = 0 error
            //    System.out.println("Hello World");
            //}
            //The solution is to use & &, because (1 = = 2) is false, so the following ((10 / 0) = = 0) will not be judged, so the compiler will not report an error
            if ((1 == 2) && (10 / 0) == 0) {
                System.out.println("Hello World");
            }
        }
        }

'&' can be used as a bitwise operator, when the expression on both sides of & is not of Boolean type, & represents bitwise operation.

3. Or (one of multiple conditions is satisfied)

In Java, both '|' and '|' are logical operators that represent and, and both represent logical transporter or. When the expressions on both sides are false, the whole operation result is false, otherwise it is true.

|(or) and (short circuit or)
(1) Similarities: in a program, when one of the expressions is true, the results are all true;
(2) Differences:
a. '|' when an expression of type true is encountered, the program will continue to walk backward and the final result will be true.
b. '|' when an expression of type true is encountered, the program does not go down, but the final result is true.

public class Test3 {
    public static void main(String args[]){
        //if((1==1)|(10/0)==0) {/ / if the program continues to run, an error will be reported when ((10 / 0) = = 0) is encountered
        //    System.out.println("Hello World");
        //}
        //Solution
        if((1==1)||(10/0)==0){//When the program encounters the first one, it stops when the result is true, and it will not continue to run
            System.out.println("Hello World");
        }
    }
}

Bit operation

Bit operations in java include: '&' (and), '| (or),' ^ '(XOR),' ~ ',' > ',' < ',' > >
Note: 'bitwise operation is not allowed for' & & ','|'

public class Test4 {
    public static void main(String args[]){
        int num1 = 9 ;
        int num2 = 11 ;
        //And
        System.out.println(num1&num2);
        //Analysis
        //      9:00000000 00000000 00000000 00001001
        //     11:00000000 00000000 00000000 00001011
        //'&' result: 00000000 00000000 00000000 00001001
        //So the result is 9
        //or
        System.out.println(num1|num2);
        //Analysis
        //      9:00000000 00000000 00000000 00001001
        //     11:00000000 00000000 00000000 00001011
        //'|' result: 00000000 00000000 00000000 000001011
        //So the result is 11
    }
}
public class Test5 {
    //Fast calculation of the third power of 2
    public static void main(String args[]){
        int num = 2 ;
        System.out.println(num<<2);//Move two places to the left
        //That is, 2:00000000 00000000 00000010
        //Change to 8:00000000 00000000 000000000
    }
}

Selection structure

1. If else structure
2.switch structure

The use of switch in java is basically the same as that of C

public class Test6 {
    public static void main(String args[]){
        int num = 1 ;
        switch(num){
            case 1:{
                System.out.println("The number is 1");
                break;
            }
            case 2:{
                System.out.println("The number is 2");
                break;
            }
            case 3:{
                System.out.println("The number is 3");
                break;
            }
            default:{
                System.out.println("No matching content");
                break;
            }
        }
    }
}

The biggest difference between Java's switch structure and C language is that Java can judge strings, which is not available in C language

public class Test6 {
    public static void main(String args[]){
        String str = "Hello World" ;
        switch(str){
            case "hello":{
                System.out.println("hello");
                break;
            }
            case "Hello World":{
                System.out.println("Hello World");
                break;
            }
            default:{
                System.out.println("No matching content");
                break;
            }
        }
    }
}
3. Circulation structure
While loop and do while Loop

While (loop judgment){
Loop statement;
Modify the end of cycle condition;
}
do{
Loop statement;
Modify the end of cycle condition;
}While (loop judgment);
It can be seen from this that the while loop is judged first and then executed. Do The while loop is executed first and then judged. Under the same conditions, do The while loop executes at least once, and the while loop does not execute once.

for cycle

For loop in java is the same as for loop in C

public class Test7 {
    public static void main(String args[]){
        int num = 0 ;
        for(int n = 1 ;n<=100;n++){
            num+=n;
        }
        System.out.println(num);
    }
}

Definition and use of method (function)

public static return value type method name (parameter type parameter variable {
Method body (several operations to be performed by this method);
[return [return value];]
}

public class Test8 {
    public static void main(String args[]){
        printf();
    }
    public static void printf(){
        System.out.println("*************");
        System.out.println("*Hello World*");
        System.out.println("*************");
    }
}
public class Test9 {
    public static void main(String args[]){
        set(100);
        set(3);
    }
    public static void set(int x){
        if(x==3){
            return;//When the return method is encountered, it ends directly. The next steps will not be executed
        }
        System.out.println("x="+x);
    }
}
Overload of method

Method overloading is simply to define multiple methods with the same name for different operations

public class Test1_1 {
    public static void main(String args[]){
        System.out.println("Two integer parameters:"+add(10,20));
        System.out.println("Three integer parameters:"+add(10,20,30));
        System.out.println("Two floating point arguments:"+add(10.2,20.3));
    }
    public static int add(int x,int y){
        return x+y;
    }
    public static int add(int x,int y,int z){
        return x+y+z;
    }
    public static double add(double x,double y){
        return x+y;
    }
}

Be careful:
1. Although the call name of the method is the same, the compiler will execute different method bodies according to the number or type of parameters it declares.
2. When the method is overloaded, it is important to distinguish different methods according to the type and number of parameters, rather than depending on the different return values.
Previously used System.out.println() and System.out.print() are also overloaded methods

Recursive call to method
public class Test1_2 {
    public static void main(String args[]){
        System.out.println(sum(100));
    }
    public static int sum(int num){
        if(num==1){
            return 1;
        }
        return num+sum(num-1);
    }
}
Published 5 original articles, praised 0, visited 44
Private letter follow

Posted by ezbie on Sat, 18 Jan 2020 04:14:58 -0800