1.&与
2.&&短路与
3.|或
4.||短路或
5.!逻辑非
6.^_逻辑异或
逻辑运算符操作的都是布尔类型的变量
&与&&的运算结果都相同,当符号左边是true是他们二者都会执行符号右边的运算
不同点:当符号左边是false时,&继续执行符号右边的运算,&&不在执行右边的运算
public class LogicTest { public static void main(String[] args) { boolean b1=true; b1=false; int c1=10; if(b1&(c1++ >0)){ System.out.println("我现在在北京"); }else { System.out.println("我现在在南京"); } System.out.println("c1="+c1); boolean b2=true; b2=false; int c2=20; if(b2&&(c2>10)){ System.out.println("我现在在北京"); }else { System.out.println("我现在在南京"); } System.out.println("c1="+c2); } }
| 与 ||
相同点:1.二者的运算结果是相同的
2.当符号左边是false时,二者都会执行符号右边的运算
不同点:当符号左边时true时,|继续执行符号右边的运算,而||不在执行符号右边的运算
boolean b3=true; int c3=10; if(b3|(c3++>0)){ System.out.println("我现在在北京"); }else{ System.out.println("我现在在南京"); } System.out.println("c3="+c3); boolean b4=true; int c4=20; if(b4||(c4++>0)){ System.out.println("我现在在北京"); }else{ System.out.println("我现在在南京"); } System.out.println("c4="+c4);
开发中推荐使用短路&&与短路||