24 Java语言基础逻辑运算符&&和&的区别

&&  与 & 的区别 

  最终的结果是一样的

  && 具有短路的效果,左边是false 右边不执行

 

同理 ||  与 |

 

 1 public class HelloWorld {
 2 public static void main(String[] args) {
 3 
 4 int x = 3;
 5 int y = 4;
 6 System.out.println((++x == 3) &( ++ y ==4)); // false 与 false 结果为false 
 7 System.out.println("x="+x);
 8 System.out.println("y="+y);  // y =5
 9 
10 
11 System.out.println((++x == 3) && ( ++ y ==4)); // false 与 false 结果为false 
12 System.out.println("x="+x);
13 System.out.println("y="+y); //y = 5  因为短路,y没有参不与运算
14 
15 }
16 }

 

posted @ 2017-01-21 11:16  panw3i  阅读(137)  评论(0编辑  收藏  举报