Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 469
Blundell Avatar asked Mar 08 '13 10:03

Blundell


People also ask

How does post increment operator work?

The post increment operator is used to increment the value of some variable after using it in an expression. In the post increment the value is used inside the expression, then incremented by one. if the expression is a = b++; and b is holding 5 at first, then a will also hold 5.

What does the ++ increment operator do?

Description. If used postfix, with operator after operand (for example, x++ ), the increment operator increments and returns the value before incrementing. If used prefix, with operator before operand (for example, ++x ), the increment operator increments and returns the value after incrementing.

What is post increment operator in C++?

2) Post-increment operator: A post-increment operator is used to increment the value of the variable after executing the expression completely in which post-increment is used. In the Post-Increment, value is first used in an expression and then incremented. Syntax: a = x++;

Can we use increment operator in if condition?

x++ is post increment; this means that the value of x is used then it is incremented. If it is so, then x=0 should be used and the answer should be true.


2 Answers

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.

like image 134
xlecoustillier Avatar answered Oct 02 '22 17:10

xlecoustillier


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.

like image 36
PermGenError Avatar answered Oct 02 '22 16:10

PermGenError