当前位置:网站首页>& 和 &&、| 和 || 的区别
& 和 &&、| 和 || 的区别
2022-07-24 08:56:00 【魔莫摸墨】
在日常编码中,我们常用的是 && 和 || ,很少会用到 & 和 | 这两个逻辑运算符,今天就简单讲讲它们两两之间的差别,一个小知识点,很简单,看了就会懂。
目录
1、逻辑运算符
| 序号 | 逻辑运算符 | 描述 | 序号 | 逻辑运算符 | 描述 |
| 1 | & | AND,与 | 3 | | | OR,或 |
| 2 | && | 短路与 | 4 | || | 短路或 |
2、& 与 && 的区别
对于 & 来说,要求所有的条件都判断;使用 &&, 如果第一个条件为 false,则后面的条件将不再判断。(下面的代码将验证以上观点)
测试代码 1 :观察被除数为 0 的情况
package second;
public class second1 {
public static void main(String[] args)
{
int x=10/0; //定义变量
System.out.println("x="+x); // 错误,被除数为 0
}
}
程序执行结果:

造成以上问题的根本原因在于被除数为 0 。下面为读者进一步讲解 & 与 && 的区别。
测试代码 2 :验证 & 的作用
package second;
public class second1 {
public static void main(String[] args)
{
if((10!=10)&(10/0==0)) {
System.out.println("条件满足");
}
}
}
程序执行结果:
![]()
造成上面错误的原因在于操作要把上面两个条件都进行了判断,当判断 (10/0==0)条件时,发生了错误。我们再来看看将 & 改为 && 的情况,见下面的代码。
测试代码 3 :验证 && 的作用
package second;
public class second1 {
public static void main(String[] args)
{
if((10!=10)&&(10/0==0)) {
System.out.println("条件满足");
}
}
}
改为 && 连接两个条件时,就没有报错了。因为 (10!=10) 条件不满足,后面的 (10/0==0) 条件就直接不会再判断了,这就是 && 的作用。
3、| 和 || 的区别
对于 | 来说,要求所有的条件都判断;使用 || ,如果第一个条件为true,则后面的条件将不再判断。(下面的代码将验证以上观点)
测试代码 4 :验证 | 的作用
package second;
public class second1 {
public static void main(String[] args)
{
if((10==10)|(10/0==0)) {
System.out.println("条件满足");
}
}
}
程序执行结果:
![]()
结果出现了报错,原理与 & 相似,使用 | ,将 (10==10)条件判断后,(10/0==0)也会被判断,除 0 出现了错误。我们再来看看 将 | 改为 || 的效果,见下面的效果。
测试代码 5 :验证 || 的作用
package second;
public class second1 {
public static void main(String[] args)
{
if((10==10)||(10/0==0)) {
System.out.println("条件满足");
}
}
}
程序执行结果:

由结果可得,if 条件中只判断了 (10==10)这个条件,在确定条件(10==10)是true的,因为是 || 连接两个条件,就没有判断后面的条件了。
差不多就是这些了,感觉还是挺好理解的,但是建议大家在日常编码中还是多用 && 和 || 吧,更加保险一些。最后,有什么问题,欢迎评论区留言哦~~
边栏推荐
- Protocol buffers 的问题和滥用
- Using OpenCV to do a simple face recognition
- Rk3566 add project under external
- Guys, what parameters can be set when printing flinksql so that the values can be printed? This later section is omitted. It's inconvenient. I read the configuration on the official website
- Leetcode102-二叉树的层序遍历详解
- Basic use of Nacos (2) -- Nacos configuration center
- pip3 带源安装大全
- Protocol buffer learning notes
- 【汇编语言实战】一元二次方程ax2+bx+c=0求解(含源码与过程截屏,可修改参数)
- How can tiktok transport videos not be streaming limited?
猜你喜欢
随机推荐
Shell script backup mongodb database
OpenCV中文文档4.0.0学习笔记(更新中……)
【情感】何为“优秀”
Larave uses sanctum for API authentication
The solution of [an error occurred while trying to create a file in the destination directory: access denied] is prompted when installing the software
Notify consumers after provider information changes in RPC
From single architecture to distributed architecture, there are many pits and bugs!
3587. Connected graph (Jilin University postgraduate entrance examination machine test question)
Take out the string in brackets
【翻译】使用gRPC和REST的微服务架构中的集成挑战
Typora prompt [this beta version of typora is expired, please download and install a new version]
C # briefly describe the application of Richter's replacement principle
Basic use of Nacos (2) -- Nacos configuration center
读写锁、共享锁、独占锁
RPC调用方如何实现异步调用:CompletableFuture
Beandefinition three ways to load beans
Advantages of using partitions
链表——19. 删除链表的倒数第 N 个结点
UE5影视动画渲染MRQ分层学习笔记
[Shangshui Shuo series] final rad New Literacies
![[FFH] websocket practice of real-time chat room](/img/9a/ffd31fe8783804d40edeca515cc96c.png)








