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