java - sequence of actions in the logical expression
问题描述
I want to ask about correct sequence "reading" of this logical expression:
(true && false | true)
I thought it returns false, because of first expression - true && false
But probably I'm doing something wrong and it's should be "reading" another way.
Can you explain what's correct way to reading it?
解决方案
Check the following table of Operators Precedence.
|
(bitwise inclusive OR) has higher precedence than &&
(logical AND), so false | true
is evaluated first.
That said, the evaluation order doesn't matter in your example. Both (true && false) | true
and true && (false | true)
return true
. In the first case it's false | true)
, which is true
. In the second case it's true && true
, which is also true
.
Now, here's an example where the operator precedence makes a difference:
System.out.println (false && true | true);
System.out.println (false && true || true);
|
has a higher precedence than &&
, but &&
has a higher precedence than ||
.
Therefore these expressions are evaluated as:
System.out.println (false && (true | true));
System.out.println ((false && true) || true);
As a result, the first returns false
and the second returns true
.
推荐阅读
- android - kotlin Android 微调器 notifyDataSetChanged()
- spring - 添加bean后出错
- android - 通过 multipart 将 byte[] 图像上传到服务器
- python-2.7 - 如何以完全隐藏的方式运行exe文件
- python - 列中具有多个标签的一种热编码
- botframework - 从 iFrame 中的 ChatBot URL 获取动态令牌
- hibernate - 如何获取列中值所在的记录数
(字符串)在休眠? - ios - 检测设备是否正在充电或已拔出
- testing - 如何在不同设备上测试移动应用
- html - 如何将 css flex 概念应用于角度材料