• Post Reply Bookmark Topic Watch Topic
  • New Topic

Some java operator questions.  RSS feed

 
Winston Liek
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get error when using this expression:

int a = 10;
int x = ++a+++a+++a;


Error was:
Unexpected type
required: variable
found: value

Can someone please explain to me why?
 
Winston Liek
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and also, how come the answer to this expression is 46:

int = 10;
a = a++ +a++ +a++ +a++;

//10 + 11 + 12 + 13 = 46 BUT on the last part of "a++", 13 becomes 14. Shouldn't 14 is the answer (disregarding 46)
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First question: You could find this out if you lookup how exactly that expression is parsed according to the rules of Java. See the Java Language Specification.

Second question: But after the last a++, the result of the right hand side is assigned to a. So a becomes 14, but then after that the value of the right hand side, 46, is assigned to a, overwriting the 14 that was in there.

These kind of expressions might be interesting to find out, but this is not really useful for everyday Java programming. Nobody who wants to write good quality, maintainable Java code would use such expressions in his or her code.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Liek wrote:I get error when using this expression:

int a = 10;
int x = ++a+++a+++a;


Error was:
Unexpected type
required: variable
found: value

Can someone please explain to me why?


The prefix operator ++ expects a variable to increment and then returns value of that variable.
So if a = 10.
++a is 11 and this is value.
So you can't write ++ ++a the same way you can't write ++10.

Winston Liek wrote:and also, how come the answer to this expression is 46:

int = 10;
a = a++ +a++ +a++ +a++;

//10 + 11 + 12 + 13 = 46 BUT on the last part of "a++", 13 becomes 14. Shouldn't 14 is the answer (disregarding 46)


The postfix operator ++ returns unmodified value of whatever it's incrementing.
So a++ +a++ +a++ +a++ translates into 10 + 11 + 12 + 13 which is 46.

Note that the last increment is lost.
 
Winston Liek
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The prefix operator ++ expects a variable to increment and then returns value of that variable.
So if a = 10.
++a is 11 and this is value.
So you can't write ++ ++a the same way you can't write ++10.


Sorry sir but can you please explain it more? Because if I try to place some whitespace in between there's no error found
int x = ++a + ++a + ++a; >>>Successfully compiled
int x = ++a+++a+++a; >>> does not compile.

BTW, shouldn't be the order for evaluation in the right hand side of equation is from right to left or left to right?


The postfix operator ++ returns unmodified value of whatever it's incrementing.
So a++ +a++ +a++ +a++ translates into 10 + 11 + 12 + 13 which is 46.

Note that the last increment is lost.


Yes sir, I think the last increment is lost

Thanks!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Liek wrote:
Sorry sir but can you please explain it more? Because if I try to place some whitespace in between there's no error found
int x = ++a+++a+++a; >>> does not compile.

my guess is the compiler gets confused. how should the compiler know if this is
++a + ++a...

or

++a++ + a...

i know I would be confused. Which is why you should never write code like this.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!