11

I read from the official tutorial of Java that prefix and postfix ++ -- have different precedences:

postfix: expr++ expr--

unary: ++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?

I know the explanation of ++d being evaluated beforehand, but if d++ has higher precedence then ++d, why isn't d++ being first evaluated? And what is more, in what case should d++ shows that it has higher precedence?

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.

5
  • 7
    Questions like this frustrate me immensely. Nobody ever writes code like System.out.println(d++ + ++d); so why do you care? Its a super-triviality.
    – Qwerky
    Commented Jun 16, 2011 at 15:11
  • 1
    @Qwerky Though no one might ever write code like this, perhaps it's for school. Teachers/professors love asking these kind of "it'll never be written like this but we're ganna test you on it anyways to see if you understand how it works" questions.
    – Kevin Zhou
    Commented Jun 16, 2011 at 15:14
  • 1
    @Qwerky Sorry for frustrating you:) As for the reason why I asked, maybe it is because curiosity. Lucky me since I am no cat.
    – zw324
    Commented Jun 16, 2011 at 15:14
  • 1
    @Kevin Teachers may love it but I can assure you this would get bounced back from code review anywhere outside of a classroom.
    – Qwerky
    Commented Jun 16, 2011 at 15:25
  • There is no postfix-notation here. It is all infix. There are postfix operators.
    – user207421
    Commented Feb 27, 2017 at 0:38

9 Answers 9

16

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

1
  • Thank you for being the first replied and enlightened me! However, I used up my max upvote today, so I guess I would have to do it tomorrow. :)
    – zw324
    Commented Jun 16, 2011 at 15:08
12

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

9

Higher 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 as (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 none, either), since ++ only works on variables, not on values (and the result is a value).

6
  • 1
    Close, but your conclusion is incorrect. It should be 1 + 3 = 4 as explained in the answers above. ++d returns the result before the increment, so although d will equal 2 after the left expression is evaluated, the left expression evaluates to 1. Commented Apr 17, 2012 at 2:47
  • Thanks for the correction ... Strange that this got unnoted for 9 months. Commented Apr 17, 2012 at 7:32
  • You're welcome! I thought it was valuable to have this explained in so many different ways here, and I think many people will find the way you presented the information helpful. Commented Apr 17, 2012 at 7:47
  • 1
    What confuses me is that all the answers google has led me to about this question talk as if "d++" and "++d" are operands. But, the Java Language Specifciation calls them prefix and postix operators. if operands are evaluated left to right, and THEN operators are applied, then (d++) + (++d) will, after evaluation of the two operands, will be ( (1)++ ) + (++(1)) -- which makes no sense to me. (what does 1++ even mean?). so, i would like to see an expanation on exactly what operands and operators get evaluated when, considering that each ++ is an operator themselves.
    – silph
    Commented Oct 9, 2016 at 21:17
  • @silph The expressions ++d and d++ are both operands to the + operator here. Themselves they both are composed of an operator ++ (a prefix and a postfix one) and its operand d. We have a recursive expression structure. Commented Oct 9, 2016 at 21:24
2

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.

2
  • I see. But could you show me a case in which the precedence is showed?
    – zw324
    Commented Jun 16, 2011 at 15:07
  • @Ziyao: In an expression such as (3 - 4 * 5), precedence dictates that this is equivalent to (3 - (4 * 5)), not ((3 - 4) * 5). Commented Jun 16, 2011 at 15:09
2

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.

0

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

0

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.

0

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.

-1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.