Difference between & and && in Java

The difference between & and && in java. Although & and && are both logical operators, they both judge that both sides are true, then the statement is true, but there are still differences when running. The following examples illustrate.

1. Example of & in Java:

public class Test2{

    public static void main(String[] args){

        int i=3;

        if((i++>5)&(i++<9)){

            System.out.println(i);

            System.out.println("Congratulations, the conditional statement is executed!");

        }

           System.out.println(i);

    }

}

 

 

Judging from the conditional judgment statement, it is not true, but i is added twice, and the initial value 3 becomes 5.

 

Second, the example of && in Java:

public class Test2{

    public static void main(String[] args){

        int i=3;

        if((i++>5)&&(i++<9)){

            System.out.println(i);

            System.out.println("Congratulations, the conditional statement is executed!");

        }    

        System.out.println(i);

    }

}

 

Judging from the conditional judgment statement, it is not established, and the variable i is only added once.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324846134&siteId=291194637