博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java运算符、 &&与&、||与|区别
阅读量:4220 次
发布时间:2019-05-26

本文共 2094 字,大约阅读时间需要 6 分钟。

 java运算符:
&和|既可以做两个boolean间运算符也可以做两个int间运算符;即:
引用
boolean & boolean   //逻辑与、Logical AND、Boolean AND
boolean | boolean   //逻辑或、Logical OR、 Boolean OR
int & int   //按位与
int | int   //按位或
当左右操作数为两个boolean时,&和|为Boolean Operators ;当左右操作数为int时,他们是Bitwise Operators。
作为Boolean Operators的&和|与&&和||的区别:
&&:Conditional AND、Short-circuit AND
||:Conditional OR、Short-circuit OR
&: Logical AND、Boolean AND
|: Logical OR、 Boolean OR
Conditional && will not evaluate the right-hand operand if the left-hand operand is false.
Conditional || omits the evaluation of the right-hand operand when the left-hand operand is true.
例子:
Conditional && and Logical &:
引用
Java代码  
  1. public static void main(String[] args) {   
  2.         int value = 8;   
  3.         int count = 10;   
  4.         int limit = 11;   
  5.         if (++value%2==0 && ++count<limit) {   
  6.         }   
  7.         System.out.println("value: " + value);   
  8.         System.out.println("count: " + count);   
  9.     }  
public static void main(String[] args) {		int value = 8;		int count = 10;		int limit = 11;		if (++value%2==0 && ++count
输出:
value: 9
count: 10
Java代码  
  1. public static void main(String[] args) {   
  2.         int value = 8;   
  3.         int count = 10;   
  4.         int limit = 11;   
  5.         if (++value%2==0 & ++count<limit) {   
  6.         }   
  7.         System.out.println("value: " + value);   
  8.         System.out.println("count: " + count);   
  9.     }  
public static void main(String[] args) {		int value = 8;		int count = 10;		int limit = 11;		if (++value%2==0 & ++count
输出:
value: 9
count: 11
Conditional || and Logical |:
引用
Java代码  
  1. public static void main(String[] args) {   
  2.     int value = 9;   
  3.     int count = 10;   
  4.     int limit = 11;   
  5.     if (++value%2==0 || ++count<limit) {   
  6.     }   
  7.     System.out.println("value: " + value);   
  8.     System.out.println("count: " + count);   
  9. }  
public static void main(String[] args) {		int value = 9;		int count = 10;		int limit = 11;		if (++value%2==0 || ++count
输出:
value: 10
count: 10
Java代码  
  1. public static void main(String[] args) {   
  2.     int value = 9;   
  3.     int count = 10;   
  4.     int limit = 11;   
  5.     if (++value%2==0 | ++count<limit) {   
  6.     }   
  7.     System.out.println("value: " + value);   
  8.     System.out.println("count: " + count);   
  9. }  
public static void main(String[] args) {		int value = 9;		int count = 10;		int limit = 11;		if (++value%2==0 | ++count
输出:
value: 10
count: 11

转载地址:http://mblmi.baihongyu.com/

你可能感兴趣的文章
Hive数据倾斜
查看>>
TopK问题
查看>>
HQL排查数据倾斜
查看>>
DAG以及任务调度
查看>>
LeetCode——DFS
查看>>
MapReduce Task数目划分
查看>>
3126 Prime Path
查看>>
app自动化测试---ADBInterface驱动安装失败问题:
查看>>
九度OJ 1091:棋盘游戏 (DP、BFS、DFS、剪枝)
查看>>
c++使用宏检测类是否包含某个函数或者变量属性
查看>>
CSS之Multi-columns的column-gap和column-rule
查看>>
CSS之Multi-columns的跨列
查看>>
CSS之浮动(一)
查看>>
CSS之浮动(二)
查看>>
AtomicInteger源码解析
查看>>
CopyOnWriteArraySet源码学习
查看>>
Openfiler 配置 NFS 示例
查看>>
Oracle 11.2.0.1 RAC GRID 无法启动 : Oracle High Availability Services startup failed
查看>>
Oracle 18c 单实例安装手册 详细截图版
查看>>
Oracle Linux 6.1 + Oracle 11.2.0.1 RAC + RAW 安装文档
查看>>