&&的使用和|运算符一起在表达式中 [英] use of && and | operators together in a expression

查看:174
本文介绍了&&的使用和|运算符一起在表达式中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

cond && cond op cond

op可以是&&||

Qn-对于短路(&&)运算符,如果第一个条件为假,则不评估(整个)右侧部分,或者仅对&&之后的第二个条件进行评估

Qn- For short circuit (&&) operator if the first cond is false then right part (whole) is not evaluated or just the second cond after && is not evaluated

为什么下面两个表达式的结果不同?

Also why the result of following two expressions different?

(2 > 3 && 5 < 2 || 3 > 2) => True

(2 > 3 && 5 < 2 | 3 > 2) => False

我们不能在单个表达式中使用短路运算符和标准运算符吗??

Can't we use short circuit operator and standard operators in a single expression...?

推荐答案

结果不同,因为||| 具体来说,|的优先级高于&&,而||的优先级低于&&.

Specifically, | has higher precedence than &&, whereas || has lower precedence than &&.

          System.out.println(2 > 3 && 5 < 2 || 3 > 2);    // true
          System.out.println(2 > 3 && 5 < 2 | 3 > 2);     // false
          System.out.println(2 > 3 && (5 < 2 | 3 > 2));   // false
          System.out.println((2 > 3 && 5 < 2) | (3 > 2)); // true

这篇关于&amp;&amp;的使用和|运算符一起在表达式中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆