3

As a preface, I am using eclipse 3.7.2 on Mint 12x64

Suppose you have the given fields:

tail = 10;
capacity = 10;

Now suppose you were to execute this statement:

tail++ %= capacity;

Why is the statement illegal? Is the statement ambiguous? To me it seems that it would evaluate in the an order such as:

  • tail = modulus capacity
  • tail increments by one
ahodder
  • 11,353
  • 14
  • 71
  • 114

2 Answers2

6

The result of the expression tail++ is a value, not a variable. From the JLS, Section 15.14.2:

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

You can't assign to a value, only to a variable (or field).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    That makes sense now. The statement was completely based on my thought that the variable was returned not the value of the variable. Thanks for the link. – ahodder Mar 27 '13 at 16:50
5

The reason why your sample does not compile is because tail++ is a value, not a variable. The ++ operator takes a variable (and increments it), and then returns a value, which you then try to assign to. You can only assign to variables, hence the compiler error. If you want to make your sample work, you could try:

tail %= capacity;
tail++;
feralin
  • 3,268
  • 3
  • 21
  • 37