• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SCJP : query regarding increment/decrement operator.

 
ankur trapasiya
Ranch Hand
Posts: 160
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello... I am preparing for SCJP and i found one difficulty regarding operators.

for e.g. in a compound statement like a[i++][++i] or statements like this .... (assume i as integer).

so i am confused about that when actually value of i will be incremented here and how this entire statement will be evaluated by the compiler. In Kathy sierra and Bert Bates book there is very less details are given . can any one prefer any material to understand this or can make me understand this topic ?

Thank you in advance...
 
Matthew Brown
Bartender
Posts: 4565
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this sort of case I'd suggest trying it to see what happens. Try this (not checked):
 
Ahmed Attyah
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i++ : Simply i++ means it reads the value then increments it which means it will return the original value but "i" itself incremented




++i : it increments i then reads it , it will retrun value of increment



so, both increment the value of i but the difference is in return value , i++ returns original value , ++i return value after increment.

Note: i don't think this is the appropriate forum for SCJP Questions
 
ankur trapasiya
Ranch Hand
Posts: 160
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear ahmed. i know pre-increment and post increment operations pretty good but the thing i wanted to know was that the order in which these operations are carried out..

e.g. int j =12; i= ++j + j++ * ++j ;

something like this. that's why i asked this question to know the flow this execution...

Thanks for your posts.
 
Imad Aydarooos
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int j =12; i= ++j + j++ * ++j ;
the order is from left to right:
1- First all your j's will be inialized as follws i = 13 + 13 * 15. and you end up with j=15
2- the arithmatic operation carried out as i = 208

(why all these smiles to someone who want to let us know more)

 
ankur trapasiya
Ranch Hand
Posts: 160
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey Imad...!!! Thanks a lot... my doubt got cleared and please dont mind putting similes in my lines is my habit .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic