当前位置:网站首页>& 和 &&、| 和 || 的区别
& 和 &&、| 和 || 的区别
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的,因为是 || 连接两个条件,就没有判断后面的条件了。
差不多就是这些了,感觉还是挺好理解的,但是建议大家在日常编码中还是多用 && 和 || 吧,更加保险一些。最后,有什么问题,欢迎评论区留言哦~~
边栏推荐
- Advantages of using partitions
- Redis learning - Introduction to redis and NiO principles
- Unity解决Package Manager“You seem to be offline”
- Taking advantage of the momentum, oceanbase promotes the lean growth of digital payment
- Basic use of Nacos (2) -- Nacos configuration center
- C语言练习题目+答案:
- 链表——19. 删除链表的倒数第 N 个结点
- 使用分区的优点
- 脉脉网友出了道 Go 面试题,你能答对吗?
- 【情感】何为“优秀”
猜你喜欢

Local warehouse associated with remote warehouse

C language practice questions + Answers:

Leetcode94-二叉树的中序遍历详解

Matlab各函数说明

Data collection solution for forestry survey and patrol inspection

JS string interception

Route interceptor

Source code analysis of BlockingQueue (arraybq and linkedbq)
![[FFH] openharmony gnawing paper growth plan -- Application of cjson in traditional c/s model](/img/a5/a8f4371a83fbd38c40aa7ba56a36d3.png)
[FFH] openharmony gnawing paper growth plan -- Application of cjson in traditional c/s model

From single architecture to distributed architecture, there are many pits and bugs!
随机推荐
脉脉网友出了道 Go 面试题,你能答对吗?
Office fallback version, from 2021 to 2019
Interviewer: man, how much do you know about the read-write lock of go language?
Online lover
Sed add content after a line
C# 简述里氏替换原则的应用
office回退版本,从2021到2019
Redis learning - Introduction to redis and NiO principles
[FFH] websocket practice of real-time chat room
PXE principle and configuration
链表——19. 删除链表的倒数第 N 个结点
Local warehouse associated with remote warehouse
Why is TCP a triple handshake
Xtrabackup realizes full backup and incremental backup of MySQL
Opencv Chinese document 4.0.0 learning notes (updating...)
table-rowspan
How to integrate and use log4net logging plug-in in vs2019 class library
【我的创作一周年纪念日】爱情是需要被纪念的,创作也是
Unity solves the package manager "you see to be offline"
How to import CAD files into the map new earth and accurately stack them with the image terrain tilt model