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

11

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

Embryologist answered 8/3, 2013 at 10:29 Comment(9)
A side remark: Judging from how you named your variables, you seem to assume that int oneA = zero++; assigns one to oneA. This is incorrect. The post increment operator returns the old (non-incremented) value. After int oneA = zero++;, oneA is 0 and zero is 1.Empiric
@Empiric true! I spotted this in my real code, this was just an exampleEmbryologist
So in your code you wouldn't just automatically write int oneC = getInt() + 1; ?Z
Two errors in code -- one could be oversight; the other is the misconception that is the basis of the question. NARQ based on a misconception.Usa
@Usa ++ confuses. there is lots of question on ++ Once solution done it looks simple.Madea
@Usa : the fact the answer to a question is obvious to you is not reason enough for the question to be worthless. ++ is really confusing for a lot of people, and most of those for which it is not have made mistakes with it and learned from them.Lights
@Lights I did not say the answer is obvious, let alone to me. I said there's no question in the first place. It's NARQ. See X.L.Ant's answer -- these are fundamental concepts and nothing specially applicable to the plus-plus operator. No offense meant.Usa
i am prefer to this superior answer [(example & theory)][1] [1]: https://mcmap.net/q/323303/-in-java-why-can-39-t-i-write-i-or-iDevoice
I don't think this question is useful, because it can't be asked by someone who actually understands what ++ means. If you ask yourself "why is there a ++, anyway, when +1 is just as easy to write?", and can answer correctly, then you already know the problem with the code in this question.Newsprint
K
150

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.

Kalina answered 8/3, 2013 at 10:32 Comment(7)
Ah of course duh, because it's doing an assignment then B & C make no sense yes!Embryologist
+1 , i just want to add small comment about this answer , 'i' is a memory allocation holds a value and have an address ,so i++ mean that 1-go to the address on the memory 2- get the value in this address 3-increment it 4-reallocate the value again in this address 'i=i+1' , so when we not initialize the variable like :int x , then x++; we get error because there is no value in this address , in case of 0++ , yes it makes no sense , because 0 is just a valueVolcanology
Actually, i = i + 1 seems to be equivalent to ++i, not i++. (I was actually a bit shocked to see that assignments result in values in Java)Fletcher
@TV'sFrank you are right, i=i+1 is equivalent to ++i. And assignment has a value, just as in C and alot of other langauges. Thus you can write stuff like i=j=0;Unblinking
You're right. But in this case, the result is the same : 0 = 0 ; 0 + 1; or getInt() = getInt(); getInt() + 1; doesn't make more sense :)Kalina
That i++ is eq to i = i + 1 doesn't hold for everything.Incorporator
Good answer, but it could be better if you explain about lvalues.Liechtenstein
P
29

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.

Polymer answered 8/3, 2013 at 10:32 Comment(0)
M
14

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.

Modulus answered 8/3, 2013 at 10:32 Comment(0)
U
10

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

Unblinking answered 8/3, 2013 at 10:35 Comment(0)
H
5

Both B and C make the compiler say:

unexpected type, required: variable, found: value

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

Haase answered 8/3, 2013 at 10:32 Comment(0)
L
4

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!
Liverpool answered 8/3, 2013 at 10:34 Comment(3)
so if the method was called setInt , would it work to increment it ??Elenor
@HussainAkhtarWahid of course not, what I mean is when you want to modify some variable you could do that via a setter not a getter. Updayed my answer.Liverpool
++ for this getY() = getY() + 1; // invalid!Madea
R
3

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.

Retrospect answered 8/3, 2013 at 10:34 Comment(1)
+ nice answer what about ++(i|i) ? because i | i always i also i & i==iMadea
D
2

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.

Dice answered 8/3, 2013 at 10:32 Comment(0)
H
1

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.

Headliner answered 15/3, 2013 at 18:11 Comment(0)
A
0

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

Aristophanes answered 13/3, 2013 at 6:33 Comment(0)
C
0

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

Cereal answered 19/3, 2013 at 11:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.