Bitwise AND (&) expression in Java

孤街醉人 提交于 2019-12-02 04:48:28

The evaluation order is well-defined in the specification:

The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right.

The HotSpot optimizer should not make an optimization that results in expr2 being evaluated before expr1 if this changes the result. If it does this it is a bug.

Note also it says:

It is recommended that code not rely crucially on this specification.

Your code could be rewritten more clearly as follows:

int a = expr1;
int b = expr2;
int result = a & b;

JLS 3rd edition section 15.7 talks about evaluation order from left to right but asks not to rely on it crucially (except for short circuit ones)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!