Operator textbook series (three) - Java logical operator

Operator textbook series (three) - Java logical operator

More, click here to learn, sign up for

Logical operators
Step 1: Road and long and short circuit and
Step 2: a long and a short circuit or path or
Step 3: Inversion
Step 4: ^ XOR

Example 1: Long and short-circuited with the passage
regardless of a long path and a short circuit or
both sides of the operation unit are Boolean values
are true, true only for the
any false, it is false
distinction
long way on both sides, operation will be
short-circuited As long as the first one is false, a second operation is not performed

And short and long road and

public class HelloWorld {
    public static void main(String[] args) {
        //长路与  无论第一个表达式的值是true或者false,第二个的值,都会被运算
        int i = 2;
        System.out.println( i== 1 & i++ ==2  ); //无论如何i++都会被执行,所以i的值变成了3
        System.out.println(i);
         
        //短路与 只要第一个表达式的值是false的,第二个表达式的值,就不需要进行运算了
        int j = 2;
        System.out.println( j== 1 && j++ ==2  );  //因为j==1返回false,所以右边的j++就没有执行了,所以j的值,还是2
        System.out.println(j);     
         
    }
}

Example 2: Long and short circuit or path, or
whether long or short circuit or path, or
both sides of the operation unit are Boolean values
are false, it is false
independently is true, the true
difference
long path operation or both sides will be
short-circuited or As long as the first one is true, the second operation is not performed

Long and short circuit or road or

public class HelloWorld {
    public static void main(String[] args) {
        //长路或  无论第一个表达式的值是true或者false,第二个的值,都会被运算
        int i = 2;
        System.out.println( i== 1 | i++ ==2  ); //无论如何i++都会被执行,所以i的值变成了3
        System.out.println(i);
         
        //短路或 只要第一个表达式的值是true的,第二个表达式的值,就不需要进行运算了
        int j = 2;
        System.out.println( j== 2 || j++ ==2  );  //因为j==2返回true,所以右边的j++就没有执行了,所以j的值,还是2
        System.out.println(j);     
         
    }
}

Example 3: inverted
inverted!
True to false
false to true

public class HelloWorld {
    public static void main(String[] args) {
        boolean b = true;
         
        System.out.println(b); //输出true
        System.out.println(!b);//输出false
         
    }
}

Example 4: ^ XOR
XOR ^
different, returns true
the same, false

XOR ^

public class HelloWorld {
    public static void main(String[] args) {
        boolean a = true;
        boolean b = false;
         
        System.out.println(a^b); //不同返回真
        System.out.println(a^!b); //相同返回假
 
    }
}
Published 32 original articles · won praise 182 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_44092440/article/details/102971098