John Assymptoth
John Assymptoth

Reputation: 8507

In Java, why can't I write i++++ or (i++)++?

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?

Upvotes: 7

Views: 5056

Answers (10)

Dhruv Raval
Dhruv Raval

Reputation: 5034

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 ++/--

Upvotes: 1

Ajay Bhojak
Ajay Bhojak

Reputation: 1159

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

Upvotes: 4

Jeshurun
Jeshurun

Reputation: 23186

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

i+=2

There.

Upvotes: 0

Kylar
Kylar

Reputation: 9334

i++ is basically a shortcut for:

(i = i+1)

And it wouldn't make any sense to write:

(i = i+1)++;

right? :)

Upvotes: 2

Joey
Joey

Reputation: 354784

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.

Upvotes: 23

Zeki
Zeki

Reputation: 5286

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?

Upvotes: 0

Chris Dodd
Chris Dodd

Reputation: 126478

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.

Upvotes: 5

JOTN
JOTN

Reputation: 6317

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.

Upvotes: 8

Péter Török
Péter Török

Reputation: 116306

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.

Upvotes: 1

zod
zod

Reputation: 12437

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

increment it twice!!

Use Looping

Increment inside a loop :)

Upvotes: 0

Related Questions