简体   繁体   English

Java中的++和 - 运算符的优先级

[英]Precedence of ++ and — operators in Java

I read from the official tutorial of Java that prefix and postfix ++ -- have different precedences: 我从Java的官方教程中读到前缀和后缀++ - 具有不同的优先级:

postfix: expr++ expr-- 后缀:expr ++ expr--

unary: ++expr --expr +expr -expr ~ ! 一元:++ expr --expr + expr -expr~!

Operators 运营商

According to the tutorial, shouldn't this 根据教程,不应该这样

d = 1; System.out.println(d++ + ++d);

print out 6 ( d++ makes d 2, ++d makes it 3) instead of 4? 打印6( d++使d 2, ++d使它成为3)而不是4?

I know the explanation of ++d being evaluated beforehand, but if d++ has higher precedence then ++d , why isn't d++ being first evaluated? 我知道预先评估++d的解释,但如果d++优先级高于++d ,为什么不首先评估d++ And what is more, in what case should d++ shows that it has higher precedence? 更重要的是,在什么情况下d++应该表明它具有更高的优先级?

EDIT: 编辑:

I tried the following: 我尝试了以下方法:

d = 1; System.out.println(++d * d++);

It returns 4. It seems that it should be 2*2, instead of 1*3. 它返回4.它似乎应该是2 * 2,而不是1 * 3。

The inside of the println statement is this operation (d++) + (++d) println语句的内部是这个操作(d ++)+(++ d)

  1. It is as follows, the value of d is read (d = 1) 如下,读取d的值(d = 1)
  2. current value of d (1) is put into the addition function 将d(1)的当前值加入加法函数中
  3. value of d is incremented (d = 2). d的值递增(d = 2)。

  4. Then, on the right side, the value of d is read (2) 然后,在右侧,读取d的值(2)

  5. The value of d is incremented (now d = 3) d的值递增(现在d = 3)
  6. Finally, the value of d (3) is put into the addition function 最后,将d(3)的值放入加法函数中

    thus 1 + 3 results in the 4 因此1 + 3导致4

edit: sorry for the format, I'm rather bad at using the list haha 编辑:抱歉格式,我很擅长使用列表哈哈

The key is what is returned from the operation. 关键是从操作返回的内容。

  • x++ changes the value of x, but returns the old x. x ++更改x的值,但返回旧的x。
  • ++x changes the value of x, and returns the new value. ++ x更改x的值,并返回新值。
d=1
System.out.println(d++ + ++d); // d is 1
System.out.println(1 + ++d); // d is 2
System.out.println(1 + 3); // d is 3

Prints 4 打印4

Different precedence does not mean will be evaluated first . 不同的优先权并不意味着将首先评估

It means the expressions will be grouped in this way . 这意味着表达式将以这种方式分组

In this case, d++ + ++d will be grouped (d++) + (++d) , and this binary expression will be evaluated in this order: 在这种情况下, d++ + ++d将被分组(d++) + (++d) ,并且将按以下顺序计算此二进制表达式:

  • left operand d++ . 左操作数d++ This subexpression consists of a postfix increment operator and a variable, so it has those two effects: 此子表达式由后缀增量运算符和变量组成,因此它具有以下两种效果:
    • The subexpression's value is 1 子表达式的值为1
    • the variable is updated: d = 2 变量更新: d = 2
  • right operand ++d . 右操作数++d This subexpression consists of a prefix increment operator and a variable, so it has those two effects: 此子表达式由前缀增量运算符和变量组成,因此它具有以下两种效果:
    • The variable is updated: d = 3 变量更新: d = 3
    • The subexpression's value is 3 子表达式的值为3
  • operator + is evaluated, using the values of the two operands. 使用两个操作数的值评估operator +
    • Thus the expression value is 1 + 3 = 4. 因此表达值为1 + 3 = 4。

The different precedence between the prefix and postfix forms of ++ would only be seen in ++d++ , which will be interpreted as ++(d++) - and this has no meaning ( (++d)++ has no one, either), since ++ only works on variables, not on values (and the result is a value). ++的前缀和后缀形式之间的不同优先级只能在++d++看到,它将被解释为++(d++) - 这没有任何意义( (++d)++没有任何一个,或者),因为++只适用于变量,而不适用于值(结果是值)。

This is not about precedence, it's about evaluation order. 这不是关于优先权,而是关于评估顺序。 d++ evaluates to 1 , but then d is incremented. d++计算结果为1 ,但随后d递增。 ++d increments d , and then evaluates to 3. ++d递增d ,然后计算结果为3。

See Why is this Java operator precedence being ignored here? 请参阅此处为何忽略此Java运算符优先级? .

It boils down to the fact that the postfix operator is being evaluated first, but returns the original value of the variable , as designed. 归结为后缀运算符首先被计算,但返回变量的原始值 ,如设计的那样。 So, for the purposes of your operation: 因此,出于您的操作目的:

(d++ + ++d)

Processes as:
1. d++ evaluates, returning the original value of 1 but incrementing d to 2
2. ++d evaluates, incrementing the value of 2 TO 3, and returning 3
3. +   evaluates, resulting in 1 + 3

The confusion is not in the order of precedence for the tokens to be evaluated, you've got that right. 对于要评估的令牌,混淆不是优先顺序,你已经做到了。 The real problem is in the understanding of the functional difference between the postfix and prefix operators. 真正的问题在于理解后缀和前缀运算符之间的功能差异。

I went through all the explanations from top ..According to understanding following code should give 11.0 then y it gives 10.0 double x = 4.5; 我经历了从顶部的所有解释..根据理解下面的代码应该给11.0然后y它给10.0倍x = 4.5; x = x + ++x; x = x + ++ x; // x gets the value 10.0. // x得到值10.0。

d has value 1 d的值为1

d++ is evaluated; d ++被评估; it's value is 1 and d is now 2 (post++ returns value before increment) 它的值是1,d现在是2(后++在增量前返回值)

++d is evaluated; ++ d被评估; it's value is 3 and d is now 3 (++pre returns value after increment) 它的值是3,d现在是3(增量后的++预返回值)

1 + 3 = 4 1 + 3 = 4

System.out.println(d++ + ++d); System.out.println(d ++ + ++ d);

Here's how it goes: 这是怎么回事:

++d is executed, so d is now 2. ++ d被执行,所以d现在是2。

d + d is executed, which equals 4. 执行d + d,等于4。

The value 4 is given to System.out.println() 值4被赋予System.out.println()

d++ is executed, so now d is 3. d ++被执行,所以现在d是3。

This is a common error. 这是一个常见的错误。 Even in the oracle certification guide, they say the Post increment and decrement operators have a higher other of precedence then the Pre operators. 即使在oracle认证指南中,他们也说Post增量和减量运算符的优先级高于Pre运算符。

I believe this is a mistake that many people have carried along for a while. 我相信这是一个许多人已经存在一段时间的错误。 After experimenting with several expressions. 在尝试了几个表达式之后。 I believe I can confidently say that the pre & post operators have the same other of precedence and are evaluated from left to right. 我相信我可以自信地说前置和后置运算符具有相同的优先级,并且从左到右进行评估。

You could experiment by evaluating the following examples to confirm for yourself. 您可以通过评估以下示例进行实验,以便自己确认。

int y = 4;
int x = --y * 2 + 3 + y-- * 2;      // x is 15

int y = 4;
int x = --y * 2 + 3 + y++ * 2;      // x is 15

In addition to the other comments, I suggest you have a look at sequence points, as some of this stuff can lead to undefined behaviours, though I think your case is defined for java. 除了其他评论之外,我建议你看看序列点,因为这些东西中的一些可能导致未定义的行为,尽管我认为你的案例是为java定义的。

What does x[i]=i++ + 1; 什么是x[i]=i++ + 1; do? 做?

http://www.angelikalanger.com/Articles/VSJ/SequencePoints/SequencePoints.html http://www.angelikalanger.com/Articles/VSJ/SequencePoints/SequencePoints.html

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

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