File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

++ operator

 
Robbie kyodo
Ranch Hand
Posts: 97
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int y = ++x + x++;
if x = 2
can somebody explain ?
 
Yi Meng
Ranch Hand
Posts: 270
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Robbie kyodo:
int y = ++x + x++;
if x = 2

y=6
++x: increase x by 1 and then evaluate x, so this expression will be of vaule 3 and x is also 3
x++: (remember x is 3 already by ++x!)evaluate x and then increase x by 1. so the expression will be of value 3 and x will be 4.
acording the above explanation, both expressions are of value 3 and thus y=3+3=6....
you may try:


what's x?
 
Robbie kyodo
Ranch Hand
Posts: 97
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int x=2; x += ++x + x++;
x = 2 + 3 + 3
8 ? is it right ?
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Robbie
In java intial evaluation order is left to right. So now evaluate ++x + x++ which gives us 3 + 3; but now makes x = 4. when first ++x is encountered it gives 3 then the value of x which is now three is now put in x, so now x also gets three, now x is incremented and now x will contain 4. So y will be 3+3=6. The evaluation order for = is right to left.
int x=2; x += ++x + x++;
x = 2 + 3 + 3
8 ? is it right ?

Right.
[ May 26, 2003: Message edited by: Anupam Sinha ]
 
Yi Meng
Ranch Hand
Posts: 270
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Yi Meng
Ranch Hand
Posts: 270
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
8 is correct.
there is no such an operator =+. To me it is just the same as = .
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Realize that order of operations and precedence isn't tested on the SCPJ exam. So I'd recommend concentrating more on inner classes and threads and fun stuff like that. In real life you'll use lots of ( )'s to make the precedence in your arithmatic to make things clear.
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robbie-
I goofed. I was editing / deleting a post of mine in this thread, and I think the UBB got a little mixed up and it accidently deleted YOUR post instead of mine. My apologies!!
- Jess
 
Robbie kyodo
Ranch Hand
Posts: 97
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I repost my message again

I think I mistype
x =+ ++x + x++
instead of += I typed =+
it still compiles. why ?
 
Yi Meng
Ranch Hand
Posts: 270
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Robbie kyodo:
I repost my message again

I think I mistype
x =+ ++x + x++
instead of += I typed =+
it still compiles. why ?

I think for =+, it is not a compound operator, but rather a combination of operators.
form maths, we know that +5 is positive five and is equal to 5 and +(-5) is still -5.
so op1=+op2 is just the same as op1=op2, op1 and op2 are compatible numeric values here.
something strange as y=+-5; is by all means valid in java. it is just the same as y= + (-5); and further more the same as y=-5;
hope it helps.........(help myself as well )
[ May 27, 2003: Message edited by: Yi Meng ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic