Use of Boolean type operations &=,|= and ^= in Java

Source: Internet
Author: User

today, when you code review your colleagues, you see a more playful notation. "Flag &= false;", at first glance, still feel he wrote wrong, but the program can run normally, hurriedly go to Baidu, look at this writing exactly is how one thing, originally this and ordinary common-=,+= and so on is the same, but usually use less only, This is really a lifting cock silk feelings of the weapon, at least can not see this kind of writing do not know what the meaning of it.

The code is as follows:

public class Mainmethod {public static void main (string[] args) {Boolean flag = true;        Flag &= true;        System.out.println ("true\t&=\ttrue\t==>\t" + flag);        Flag = true;        Flag &= false;         System.out.println ("true\t&=\tfalse\t==>\t" + flag);        Flag = false;        Flag &= true;         System.out.println ("false\t&=\ttrue\t==>\t" + flag);        Flag = false;        Flag &= false;                 System.out.println ("false\t&=\tfalse\t==>\t" + flag+ "\ n");        Flag = true;        Flag |= true;        System.out.println ("true\t|=\ttrue\t==>\t" + flag);        Flag = true;        Flag |= false;         System.out.println ("true\t|=\tfalse\t==>\t" + flag);        Flag = false;        Flag |= true;         System.out.println ("false\t|=\ttrue\t==>\t" + flag);        Flag = false;        Flag |= false;                 System.out.println ("false\t|=\tfalse\t==>\t" + flag+ "\ n"); System.out.println ("^= the same is true, different is false");        Flag = true;        Flag ^= true;        System.out.println ("true\t^=\ttrue\t==>\t" + flag);        Flag = true;        Flag ^= false;         System.out.println ("true\t^=\tfalse\t==>\t" + flag);        Flag = false;        Flag ^= true;         System.out.println ("false\t^=\ttrue\t==>\t" + flag);        Flag = false;        Flag ^= false;     System.out.println ("false\t^=\tfalse\t==>\t" + flag); }}


The results of the implementation are as follows:
true &= true ==> truetrue &= false ==> falsefalse &= true ==> falsefalse &= false ==> falsetrue |= true ==> truetrue |= false ==> truefalse |= True ==> truefalse |= false ==> false^= The same is true, the difference is falsetrue ^= true ==> falsetrue ^= false ==> truefalse ^= True ==> truefalse ^= false ==> false

Use of Boolean type operations &=,|= and ^= in Java

Related Article

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.