论坛首页 Java企业应用论坛

difference between & and && for boolean

浏览 1550 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-07-30  

Core Java chapter3.5.3讲到,

 

&和|应用于boolean运算与&&和||的运算类似,只是不按 “短路”方式计算。

 

Bytecode Outline 插件便可以看到执行的差别

 

Bit And

 

public class TestBitAnd {
    
    public static void main(String[] args) {
        boolean a = false;
        boolean b = true;
        
        boolean bitAnd  = a&b;
        //System.out.println(a +"&" +b+":" + bitAnd);
    }
}

//bytecode
/*
public class corejava_ch3_5_3/TestBitAnd {

  // compiled from: TestBitAnd.java

  // access flags 0x1
  public <init>()V
   L0
    LINENUMBER 21 L0
    ALOAD 0
    INVOKESPECIAL java/lang/Object.<init>()V
    RETURN
   L1
    LOCALVARIABLE this Lcorejava_ch3_5_3/TestBitAnd; L0 L1 0
    MAXSTACK = 1
    MAXLOCALS = 1

  // access flags 0x9
  public static main([Ljava/lang/String;)V
   L0
    LINENUMBER 24 L0
    ICONST_0
    ISTORE 1
   L1
    LINENUMBER 25 L1
    ICONST_1
    ISTORE 2
   L2
    LINENUMBER 27 L2
    ILOAD 1                          
    ILOAD 2                          
    IAND                              //载入两个变量,进行运算
    ISTORE 3
   L3
    LINENUMBER 29 L3
    RETURN
   L4
    LOCALVARIABLE args [Ljava/lang/String; L0 L4 0
    LOCALVARIABLE a Z L1 L4 1
    LOCALVARIABLE b Z L2 L4 2
    LOCALVARIABLE bitAnd Z L3 L4 3
    MAXSTACK = 2
    MAXLOCALS = 4
}*/

 

Logical And

public class TestLogicalAnd {
    
    public static void main(String[] args) {
        boolean a = false;
        boolean b = true;
        
        boolean logicalAnd = a&&b;
        //System.out.println(a +"&&" +b+":" + logicalAnd);
    }
}

//bytecode
/*
 public class corejava_ch3_5_3/TestLogicalAnd {

  // compiled from: TestLogicalAnd.java

  // access flags 0x1
  public <init>()V
   L0
    LINENUMBER 21 L0
    ALOAD 0
    INVOKESPECIAL java/lang/Object.<init>()V
    RETURN
   L1
    LOCALVARIABLE this Lcorejava_ch3_5_3/TestLogicalAnd; L0 L1 0
    MAXSTACK = 1
    MAXLOCALS = 1

  // access flags 0x9
  public static main([Ljava/lang/String;)V
   L0
    LINENUMBER 24 L0
    ICONST_0
    ISTORE 1
   L1
    LINENUMBER 25 L1
    ICONST_1
    ISTORE 2
   L2
    LINENUMBER 27 L2
    ILOAD 1
    IFEQ L3                              //判断是否为0,是则goto L3
    ILOAD 2
    IFEQ L3
    ICONST_1
    GOTO L4
   L3
    ICONST_0
   L4
    ISTORE 3
   L5
    LINENUMBER 29 L5
    RETURN
   L6
    LOCALVARIABLE args [Ljava/lang/String; L0 L6 0
    LOCALVARIABLE a Z L1 L6 1
    LOCALVARIABLE b Z L2 L6 2
    LOCALVARIABLE logicalAnd Z L5 L6 3
    MAXSTACK = 1
    MAXLOCALS = 4
}
 */
 

 

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics