简体   繁体   English

java中〜和++的优先级

[英]precedence of ~ and ++ in java

consider this code snippet 考虑这段代码

int j = 7;
System.out.println(Integer.toBinaryString(j));
j = ~j++;
System.out.println(Integer.toBinaryString(j));

prints 版画

111
11111111111111111111111111111000

what i am expecting to see 我期待看到的

111
11111111111111111111111111111001

first i thought it might be the precedence of ~ and ++ 首先我认为它可能是〜和++的优先级

if the ~ is evaluated before ++ the answer will be 如果在++之前评估〜,答案就是

11111111111111111111111111111001

else if the ++ is evaluated before ~ 否则,如果在〜之前评估++

11111111111111111111111111110111

I searched Oracle tutorials but I couldn't find the answer. 我搜索了Oracle教程,但我找不到答案。 Can anyone explain this behavior? 谁能解释这种行为?

Don't forget that the '++' post-increment operator returns the value of j before the increment happened. 不要忘记'++'后增量运算符在增量发生之前返回j的值。 That is, if 'j' is 7, then 'j++' sets j to 8, but returns 7 . 也就是说,如果'j'是7,那么'j ++'将j设置为8, 但返回7 ~7 is then the output that you saw, the number ending in three 0 bits. 然后~7是你看到的输出,数字以三个0位结尾。

The '++' post-increment operator can only operate on so-called "L-values". '++'后增量运算符只能在所谓的“L值”上运行。 An L-value is a value that actually exists somewhere that code can logically reference -- a variable, or an array element, a parameter or a class field. L值是实际存在于代码可以逻辑引用的某个值 - 变量,数组元素,参数或类字段。 As soon as you take the value of of an L-value and you apply some numerical operation to it, you get an R-value. 只要取出L值的值并对其应用某些数值运算,就会得到一个R值。 R-values are just the value, and they don't refer to any persistent storage where a result could be put. R值只是值,它们不是指可以放置结果的任何持久存储。 You can assign to L-values but not to R-values -- and so if you tried to '++' an R-value, you would get a compile error. 您可以分配L值但不分配R值 - 因此,如果您尝试'+'R值,则会出现编译错误。

If the '~' operator went first, then you'd be ++-ing an R-value, as in (~j)++. 如果'〜'运算符先行,那么你就像在(~j)++中那样使用一个R值。 This would not compile. 这不会编译。 The fact that the code compiles at all means that the precedence is the other way: ~(j++). 代码编译的事实意味着优先级是另一种方式:〜(j ++)。

Parentheses like this is the simplest way I know of that you can sort out precedence whenever there is any confusion: Just write three test cases: 像这样的括号是我所知道的最简单的方法,你可以在出现任何混淆时排除优先级:只写三个测试用例:

  1. The original way that you're uncertain about. 你不确定的原始方式。
  2. With parentheses forcing one order of operations. 用括号强制一个操作顺序。
  3. With parentheses forcing the other order of operations. 用括号强制执行其他操作顺序。

Run it and see whether #2 or #3 produces the same result as #1. 运行它,看看#2或#3是否产生与#1相同的结果。 :-) :-)

The code seems very brittle. 代码似乎非常脆弱。 I think that what is happening is that when the expression "~i++" is evaluated the value "~i" is extracted, "i++" is performed and then finally the assignment (overriding the previous value from "i++"). 我认为正在发生的事情是,当评估表达式“~i ++”时,提取值“~i”,执行“i ++”,然后最终执行赋值(从“i ++”覆盖先前的值)。

Both operators are right associative second degree operators, but a simple test reveals that it is the ++ executed first and the BITWISE NOT operation the latter. 两个运算符都是右关联的二次运算符,但是一个简单的测试表明它首先执行++ ,而BITWISE NOT运算后者。

int j = 7, z = 7, k = 7;
z = z++;
z = ~z;
k = ~k++;
j = ~j;
j++;
System.out.println(Integer.toBinaryString(z));
// 11111111111111111111111111111000
System.out.println(Integer.toBinaryString(j));
// 11111111111111111111111111111001
System.out.println(Integer.toBinaryString(k));
// 11111111111111111111111111111000

Unary operators ( ++1 , -- , + , - , ~ , ! ) are evaluated from right to left. 一元运算符( ++1--+-~ ! )从右到左进行计算。 Hence ++ is evaluated before ~ . 因此++~之前被评估。

First, don't do that. 首先,不要这样做。 There is no reason to bunch up operators like that into one statement, but I realize you weren't really planning to put this into live code and were just experimenting. 没有理由将这样的运算符组合成一个语句,但我意识到你并没有真正计划将它放到实时代码中并且只是在试验。 Try this page for Java operator precedence: http://bmanolov.free.fr/javaoperators.php 试试这个页面的Java运算符优先级: http//bmanolov.free.fr/javaoperators.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM