开发测试学习
目录

布尔逻辑运算符进行布尔逻辑运算,如下表所示:
op1op2op1&&op2op1||op2!op1
falsefalsefalsefalsetrue
falsetruefalsetruetrue
truefalsefalsetruefalse
truetruetruetruefalse}
·&&、‖为二元运算符,实现逻辑与、逻辑或。
·!为一元运算符,实现逻辑非。
·对于布尔逻辑运算,先求出运算符左边的表达式的值,对或运算如果为true,则整个表达式的结果为true,不必对运算符右边的表达式再进行运算;同样,对与运算,如果左边表达式的值为false,则不必对右边的表达式求值,整个表达式的结果为false。
下面的例子说明了关系运算符和布尔逻辑运算符的使用。

 publicclassRelationAndConditionOp{
  publicstaticvoidmain(Stringargs[]){
   int a=25,b=3;
   boolean d=a<b;  //d=false
   System.out.println("a<b="+d);
   int e=3;
   if(e!=0&&a/e>5)
    System.out.println("a/e="+a/e);
    int f=0;s
   if(f!=0&&a/f>5)
    System.out.println("a/f="+a/f);
   else
    System.out.println("f="+f);
  }
 }

其运行结果为:
C:\>javaRelationAndConditionOp
a<b=false
a/e=8
f=0
注意: 上例中,第二个if语句在运行时不会发生除0溢出的错误,因为e!=0为false,所以就不需要对a/e进行运算。