Java Boolean values for and and or logical operation principle

Source: Internet
Author: User

First look at the following code:

 Public classTest { Public Static voidTest () {BooleanA =true; Booleanb =false; if(A |b) {System.out.println ("|"); }        if(A &b) {System.out.println ("&"); }    }     Public Static voidMain (string[] args) {test (); }}

Output: |

| and & operators are OR and and logical operations

1 | 0 = = 1

1 & 0 = = 0

Let's take a look at the byte code of the test method

0: Iconst_11: Istore_02: Iconst_03: Istore_14: Iload_05: Iload_16: IoR7:ifeq 10:getstatic #2//Field Java/lang/system.out:ljava/io/printstream;13:LDC #3//String |15:invokevirtual #4//Method java/io/printstream.println: (ljava/lang/string;) V18: Iload_019: Iload_120: Iand21:ifeq 24:getstatic #2//Field Java/lang/system.out:ljava/io/printstream;27:LDC #5//String &29:invokevirtual #4//Method java/io/printstream.println: (ljava/lang/string;) V32:return

We're paying attention to 0-3 bytecode.

0: Put constant 1 into the stack

1: Assigns 1 to the first variable (a)

2: Put constant 0 into the stack

3: Assign a value of 0 to the second variable (b)

So the logical operation of Boolean is the logical operation of integral type 01.

7:ifeq 18 indicates

The IF () determines whether the result of the logical operation equals 0, or if it is equal to 0, the code within the parentheses is skipped to make the following code.

Java Boolean values for and and or logical operation principle

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.