Precedence of ++ and — operators in Java

笑着哭i 提交于 2019-11-29 10:01:28

The inside of the println statement is this operation (d++) + (++d)

  1. It is as follows, the value of d is read (d = 1)
  2. current value of d (1) is put into the addition function
  3. value of d is incremented (d = 2).

  4. Then, on the right side, the value of d is read (2)

  5. The value of d is incremented (now d = 3)
  6. Finally, the value of d (3) is put into the addition function

    thus 1 + 3 results in the 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 changes the value of x, and returns the new value.
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

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:

  • left operand d++. This subexpression consists of a postfix increment operator and a variable, so it has those two effects:
    • The subexpression's value is 1
    • the variable is updated: d = 2
  • right operand ++d. This subexpression consists of a prefix increment operator and a variable, so it has those two effects:
    • The variable is updated: d = 3
    • The subexpression's value is 3
  • operator + is evaluated, using the values of the two operands.
    • Thus the expression value is 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).

This is not about precedence, it's about evaluation order. d++ evaluates to 1, but then d is incremented. ++d increments d, and then evaluates to 3.

dolphy

See Why is this Java operator precedence being ignored here?.

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.

d has value 1

d++ is evaluated; it's value is 1 and d is now 2 (post++ returns value before increment)

++d is evaluated; it's value is 3 and d is now 3 (++pre returns value after increment)

1 + 3 = 4

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

Here's how it goes:

++d is executed, so d is now 2.

d + d is executed, which equals 4.

The value 4 is given to System.out.println()

d++ is executed, so now d is 3.

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; x = x + ++x; // x gets the value 10.0.

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.

What does x[i]=i++ + 1; do?

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

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.

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