Java SE(23) operator-logical operator

table of Contents

Logical Operators

&和&&

|and||


Logical Operators

Logical operators are used to operate on Boolean data , and the result is still a Boolean data .

Operator Calculation example result
& versus true&true true
true&false false
false&true true
false&false false
&& Short circuit with true&true true
true&false false
false&true true
false&false false
| or true | true true
true | false true
false | true true
false | false false
|| Short circuit or true || true true
true || false true
false || true true
false || false false
^ XOR true^true false
true^false true
false^true true
false ^ false false
! non- !true false
!false true

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Exercise:

public class TestOpe11{
	public static void main(String[] args){
		//逻辑与:&   规律:全对为对。
		System.out.println(true&true);     //true
		System.out.println(true&false);    //false
		System.out.println(false&true);    //false
		System.out.println(false&false);   //false
		//短路与:&&  规律:全对为对
		//只要左边表达式是false,右边的表达式不会进行运算,结果为false。
		System.out.println(true&&true);     //true
		System.out.println(true&&false);    //false
		System.out.println(false&&true);    //false
		System.out.println(false&&false);   //false
		
		//逻辑或:|   规律:任一为true,结果为true。
		System.out.println(true|true);     //true
		System.out.println(true|false);    //true
		System.out.println(false|true);    //true
		System.out.println(false|false);   //false
		//短路或:||  规律:任一为true,结果为true。
		//只要左边表达式是true,右边的表达式不会进行运算,结果为true。
		System.out.println(true||true);     //true
		System.out.println(true||false);    //true
		System.out.println(false||true);    //true
		System.out.println(false||false);   //false
		
		//逻辑异或:^  规律:运算符两边的布尔值不同时,结果为true。
		System.out.println(true^true);     //false
		System.out.println(true^false);    //true
		System.out.println(false^true);    //true
		System.out.println(false^false);   //false
		
		//逻辑非:!    规律:相反结果。
		System.out.println(!true);       //false
        System.out.println(!false);      //true
	}
}

 operation result:

  

  • &和&&

       The same point : & operator && expressed and operation , if and only if the operand on both sides of the operator are true , its result it is true , otherwise the result is false.

       Difference : When using & to calculate, no matter if the left side is true or false, the expression on the right will be calculated .

                     If && is used for calculation, when the left side is false , the expression on the right will not be calculated , so && is called a short-circuit and.

Exercise: 

public class TestOpe12{
	public static void main(String[] args){
		//左边为false
		int a=8;
		System.out.println((5>7)&(a++==2));   //false
		System.out.println("a="+a);       //i=9
		
		int b=8;
		System.out.println((5>7)&&(b++==2));   //false
		System.out.println("b="+b);       //左边表达式为flase,右边表达式不进行运输算 i=8
		
		//左边为true
		int m=8;
		System.out.println((5<7)&(m++==2));   //false
		System.out.println("m="+m);       //i=9
		
		int n=8;
		System.out.println((5<7)&&(n++==2));   //false
		System.out.println("n="+n);       //i=9
		
	}
}

  operation result:

  

  • |and||

       The same point : Operator | and || expressed or operation , and if the operand on both sides of operators on either side of the value is true , its result it is true , when the value of both sides are fals time E, the result is false .

       Difference : When using | for calculation, the expression on the right will be calculated regardless of whether the left side is true or false .

                     If you use || for calculation, when the left side is true , the expression on the right will not be calculated , so || is called a short-circuit or.

Guess you like

Origin blog.csdn.net/wqh101121/article/details/112560587