Why doesn't the post increment operator work on a method that returns an int?

JavaPost Increment

Java Problem Overview


public void increment(){
	int zero = 0;

	int oneA = zero++; // Compiles

	int oneB = 0++;	// Doesn't compile

	int oneC = getInt()++; // Doesn't compile
}

private int getInt(){
	return 0;
}

They are all int's, why won't B & C compile? Is it to do with the way ++ operator differs from = 0 + 1;?

> Invalid argument to operation ++/--

Java Solutions


Solution 1 - Java

i++ is an assignment to a variable i.

In your case, zero++ is an equivalent to zero = zero + 1. So 0++ would mean 0 = 0 + 1, which makes no sense, as well as getInt() = getInt() + 1.

More accurately :

int oneA = zero++;

means

int oneA = zero;
zero = zero + 1; // OK, oneA == 0, zero == 1

int oneB = 0++;

means

int oneB = 0;
0 = 0 + 1; // wrong, can't assign value to a value.

int oneC = getInt()++;

means

int oneC = getInt();
getInt() = getInt() + 1; // wrong, can't assign value to a method return value.

From a more general point of view, a variable is a L-value, meaning that it refers to a memory location, and can therefore be assigned. L in L-value stands for left side of the assignment operator (i.e. =), even if L-values can be found either on the left side or the right side of the assignment operator (x = y for instance).

The opposite is R-value (R stands for right side of the assignment operator). R-values can be used only on the right side of assignment statements, to assign something to a L-value. Typically, R-values are literals (numbers, characters strings...) and methods.

Solution 2 - Java

Because as stated in JLS:

> The result of the postfix expression must be a variable of a type that > is convertible (§5.1.8) to a numeric type, or a compile-time error > occurs.

Solution 3 - Java

getInt() is not int

getInt() returns int

++ operator does two things increment + assignment

So for ++ operator to work you need a variable to store the result of increment operation which 0 and getInt() both are not.

Solution 4 - Java

The pre- and post- operators only operate on variables or lvalues as they are called. lvalue is short for left value, i.e. something that can stand to the left in an assignment. In your example:

    zero = 1; // OK
    0 = 1; // Meaningless
    getInt() = 1; // Also meaningless

//jk

Solution 5 - Java

Both B and C make the compiler say:

> unexpected type, required: variable, found: value

So you can't increment a value, only a variable.

Solution 6 - Java

> Why doesn't the post increment operator work on a method that returns an int?

Because it is a getter method, and it doesn't make sense to change a value via getter.


int z = x + y++;

is equivalent to:

int z = x + y;
y = y + 1;

so it is not valid to have something like:

int z = x + getY()++;

which is equivalent to:

int z = x + getY();
getY() = getY() + 1; // invalid!

Solution 7 - Java

>0++

It is equivalent to 0 = 0 + 1; and certainly it is not possible.

i.e. it has to be l-value to assign to it.

>getInt()++;

Similar reason here.

Solution 8 - Java

Because 0 is a rValue (i.e. You can use it only from right of the assignment operator) not a lValue.

++ operator increments the value and sets it to itself therefore 0++ will give You an error.

Solution 9 - Java

My answer its kind of "out of the box".

When I have doubt about an operator usage, I think "which its the overloaded function equivalent" of this operator ?

I, know, that Java operators doesn't have operator overloading, its just an alternative way to make a solution.

In this case:

...
x++;
...

should be read as:

...

int /* function */ postincrement (/* ref */ int avalue)
{
  int Result = avalue;

  // reference value, 
  avalue = avalue + 1;

  return Result;
}

...
postincrement(/* ref */ x);
...

And:

...
++x;
...

...

int /* function */ preincrement (/* ref */ int avalue)
{
  // reference value, 
  avalue = avalue + 1;

  int Result = avalue;

  return Result;
}

...
preincrement(/* ref */ x);
...

So, both, versions of "++", work as a function that receives a variable parameter by reference.

So, a literal value like "0++" or a function result like "getInt()++", are not a variable references.

Cheers.

Solution 10 - Java

postincrement and preincrement can apply only with the help of variable.So the first case compile.

Solution 11 - Java

Since function return is RHS expression and pre/post increment/decrement operations can be applied to LHS expressions only.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBlundellView Question on Stackoverflow
Solution 1 - JavaxlecoustillierView Answer on Stackoverflow
Solution 2 - JavaPermGenErrorView Answer on Stackoverflow
Solution 3 - JavaApurvView Answer on Stackoverflow
Solution 4 - Javaj.karlssonView Answer on Stackoverflow
Solution 5 - Javauser1907906View Answer on Stackoverflow
Solution 6 - JavaEng.FouadView Answer on Stackoverflow
Solution 7 - JavaAzodiousView Answer on Stackoverflow
Solution 8 - JavaArsen AlexanyanView Answer on Stackoverflow
Solution 9 - JavaumlcatView Answer on Stackoverflow
Solution 10 - JavaGijoView Answer on Stackoverflow
Solution 11 - JavaGaurav PhapaleView Answer on Stackoverflow