Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java prefix/postfix operators

Why I'm getting an error for:

int i=0;
++i++;

What does this error mean?

  unexpected type ++i++;                    
  required: variable  
  found:    value
like image 428
user1802439 Avatar asked Feb 08 '26 03:02

user1802439


1 Answers

It is about lvalue (left value). Lvalue is, what on the left side of an "=" stay can. Also, to what you can give a value. For example, you can't give a value to "4", but you can to give a value to "i". The expressions of variables aren't just so lvalues (with the exception of some very esoteric programming language).

If you write "++i++", it will be interpreted as (++i)++ or ++(i++). "i" is an lvalue, but "i++" or "++i" not, because they are expressions.

In C++, with some tricky operator overloading and reference-variable tricks, the C++ compiler could be tricked to handle this correctly.

like image 142
peterh Avatar answered Feb 09 '26 18:02

peterh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!