Java logical operators

Source: Internet
Author: User
Tags joins

/*

A: What are logical operators

& With (and), | or (OR),! (non), ^ (XOR),

Logic Double and && (short circuit and), logic double or | | (Short circuit or)

Precautions:

A: Logical operators are typically used to concatenate expressions or values of type Boolean.

In Java we want to represent a number greater than 3 and less than 6, can not be written 3<x<6, should be written x>3 & X<6.

B: Expression: It is the Java syntax that joins a constant or variable with an operator.

Arithmetic expression: A + b

Comparison expression: a = = b

Assignment expression: int a = 10

What is a Boolean-type expression?

Is that the result of an expression is a Boolean type.

Comparison expressions


Conclusion

&: False on both sides as long as there is a positive result

False if False

|: Both sides as long as true result must be true

True if True

^: The same on both sides is false different is true

Same-sex repulsion, heterosexual phase suction (magnet relationship)

!: Non-(not)

False if not true

True if not false


An even number of non-obtained results for a value is itself

*/

Class operatordemo{public static void main (String[] args)  {int a =  3;int b = 4;int c = 5;        // & Usage  : And  //the left and right sides of a Boolean-type value or expression System.out.println ((a>b) & (a>c));//false &  false  =  false        system.out.println ((A &GT;B) & (A<c);//false & true   =  falsesystem.out.println (a< b) & (A&GT;C);//true  & false  =  falsesystem.out.println (A&LT;B) & (A<c));//true  &  true  =  true         system.out.println ("-------------------------------------------------------");//|   or,   or meaning    has true trueSystem.out.println ((a>b) | ( A&GT;C));//false | false  =  false        system.out.println ((a>b) | ( A&LT;C));//false | true   =  truesystem.out.println ((a<b) | ( A&GT;C));//true  | false  =  truesystem.out.println ((a<b) | ( A&LT;C));//true  |  true  =  truesystem.out.println ("----------------- --------------------------------------");//^  xor     same as false  different for trueSystem.out.println ( (a>b) ^ (a>c));//false ^ false  =  false         system.out.println ((a>b) ^ (a<c));//false ^ true   =   TrueSystem.out.println ((a<b) ^ (a>c));//true  ^ false  =   TrueSystem.out.println ((a<b) ^ (a<c));//true  ^ true  =    FalseSystem.out.println ("-------------------------------------------------------");       //Logic non-!&NBSP;&NBSP;SYSTEM.OUT.PRINTLN (! A&GT;B));//!FALSE&NBSP;=&NBSP;TRUE&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN (!! (a>b)); /!! FALSE&NBSP;=&NBSP;!TRUE&NBSP;=&NBSP;FALSE&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN (!!! (a>b)); /true}}


/*

The difference between logic double and && and logic and &


Common && in Development | |


The logical operator joins a Boolean type of

&: False if False


A: As A result

b:&& with short circuit effect

If you encounter false, the following formula is no longer executed

False to the left, not to the right.

&& Efficiency ratio & High


Common && in development

|| : After encountering true, the formula is no longer executed

*/

Class operatordemo2{public static void main (String[] args)  {/*int a =  3;int b = 4;int c = 5;//&& usage  : and  // Connecting the left and right values or expressions of type Boolean                       &       && System.out.println ((a>b) && (a>c));//false && false  =   False        system.out.println (a>b) && (a<c);//false  && true   =  falsesystem.out.println ((a<b) && (a>c)); /true  && false  =  falsesystem.out.println ((a<b) && (a &LT;C));//true  &&  true  =  true         system.out.println ("-------------------------------------------------------") *///use on int x = 3;int y = 4;//&         //system.out.println ((x++==3) & (y++==4));//true &  True         //system.out.println ("x:" +x);//4//System.out.println ("Y:" +y);//5//&&        //system.out.println (x++==3) & & (Y++==4));//true & true         // System.out.println ("x:" +x);//4//system.out.println ("Y:" +y);//5         &         //system.out.println ((++x==3) & (y++==4));// false & true =false                //x first self-added to 4  again do judgment   The first result is false  //continue to execute the right side of the symbol, first make a judgment, and then add   The second result is true          //system.out.println ("x:" +x);//4     //system.out.println ("Y: +y)//5 system.out.println ((++x==3) && (y++==4));//false & true =false                   //  X self-add to 4  and then make a judgment   the first result is false  //  &&  encounter false  behind the will not execute,  //   does not execute the right side of the symbol//because you know   logic with the conclusion    there is false then false           system.out.println ("x:" +x);//4     system.out.println ("Y:" +y);//4}}


This article from the "Clear Sky" blog, declined reprint!

Java logical operators

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.