Why Can’t i++++ or (i++)++ Be Written in Java?

decrementincrementjavajls

When I try to write a postfix/prefix in/decrement, followed by a post/prefix in/decrement, I get the following error: Invalid argument to operation ++/–.

But, according to JLS:

PostIncrementExpression:
        PostfixExpression ++

and

PostfixExpression:
        Primary
        ExpressionName
        PostIncrementExpression
        PostDecrementExpression

so writing:

PostfixExpression ++ ++

should be possible… Any thoughts?

Best Answer

Note that the raw grammar lacks any semantics. It's just syntax, and not every syntactically valid program will generally be valid. For example, the requirement that variables have to be declared before usage is typically not covered by the grammar (you can, but it's cumbersome).

Postfix-increment yields an rvalue – and just as you cannot postfix-increment literals, you cannot postfix-increment the result of i++.

Quoting from the JLS (3rd ed., page 486):

The result of the postfix increment expression is not a variable, but a value.