Pregunta

Cuando intento escribir un sufijo / prefijo / decremento, seguido de un post / prefijo / decremento, me sale el siguiente error:. argumento no válido para la operación ++ / -

Sin embargo, de acuerdo con JLS:

PostIncrementExpression:
        PostfixExpression ++

y

PostfixExpression:
        Primary
        ExpressionName
        PostIncrementExpression
        PostDecrementExpression

por lo que escribir:

PostfixExpression ++ ++

debería ser posible ... ¿Alguna idea?

¿Fue útil?

Solución

Tenga en cuenta que la gramática prima carece de semántica. Es sólo la sintaxis y no todos los programas sintácticamente válidos generalmente será válida. Por ejemplo, el requisito de que las variables tienen que ser declarado antes de su uso es generalmente no está cubierta por la gramática (que puede, pero es engorroso).

Postfix-incremento se obtiene un valor de lado derecho - y del mismo modo que no literales postfijos-incremento, no se puede postfijo de incremento del resultado de i++

.

Citando de los JLS (3 er ed, página 486.):

El resultado de la expresión de incremento postfix no es una variable, pero un valor.

Otros consejos

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top