Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

The error tells you the answer:

unexpected type
required: variable
found   : value
        (i++)++;

So, the i++ evaluates to a value while the operator requires a variable.

You can only apply ++ or -- to an expression that denotes a modifiable location (an lvalue). The RESULT of a ++ or -- is the value from the location (an rvalue -- either before or after the increment or decrement), and not itself a modifiable location. So you can't say (a++)++ any more than you can say (a+b)++ -- there's no location to be modified.

The problem with this expression i = (i++)++; is that (i++) gets resolved to a value and the definition of ++ operator says that 1 . it will increment the variable specified and will put/return a value for this whether you use Postfix or Prefix. 2. This operator requires variable whether prefix or postfix. But what's happening here is (i++) is return a value and putting it in place of (i++) and then you are having (value)++ and (value) is not the expected type for this operator as it requires a variable in place of value.

for example in Java if you compile the following code snippet you will get error as is shown after snippet:

public class A{ public void test(){ int i =0; i = (i++)++; } }

Compilation output :

A.java:4: unexpected type required: variable found : value i = (i++)++; ^ 1 error

i++ is basically a shortcut for:

(i = i+1)

And it wouldn't make any sense to write:

(i = i+1)++;

right? :)

What should be the result of such an operation? The result of i++ is (a copy of) the current value of i, and i is incremented afterwards (post). So how do you imagine incrementing the result of i++ once again? If i originally was 1, what should its value be after i++++, and what should be the result of this operation?

If you think about it, you probably realize it would be very difficult to define this properly. Since the designers of Java intended to avoid the C/C++ "undefined" traps (and since the value of such a statement is dubious at best), they probably decided to explicitly disallow it.

Must Remember: Increment & decrement (prefix & postfix) operator always work with variable not value

Here i am explain this with one exe :

int i = 0;

int j = (i++)++;

above exe.

initialize value of i with 0(zero)

in (i++) '++' work with i which is variable & after performing operation return (1)

now in (1)++ '++' work with (1) which is value not variable that is oppose to rules of java that's why compile time error generate Invalid argument to operation ++/--

What you trying to achieve by (i++)++ ?

increment it twice!!

Use Looping

Increment inside a loop :)

In i++, ++ is a postfix operator, so it returns the value of i and then increments it. What would you want (i++)++ to do? Should it return i+1 since (i++ == i )? If not wouldn't that be strange that i++=i but (i++)++ != i) consider the following expressions:

i++  ==  i;   // true
j = i++  // j gets the value of i
j++ == i //  still true since j== i

(i++) ++ ==  j ++    //  what should be the answer here?

Have you considered i+=2?

Why don't you just use the shorthand increment operator?

i+=2

There.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top