It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes SCJP : query regarding increment/decrement operator. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "SCJP : query regarding increment/decrement operator." Watch "SCJP : query regarding increment/decrement operator." New topic

SCJP : query regarding increment/decrement operator.

ankur trapasiya
Ranch Hand

Joined: Sep 24, 2010
Posts: 160

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

Joined: Apr 06, 2010
Posts: 4543

In this sort of case I'd suggest trying it to see what happens. Try this (not checked):
Ahmed Attyah

Joined: Apr 02, 2010
Posts: 4
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

Coming for you SCJP
ankur trapasiya
Ranch Hand

Joined: Sep 24, 2010
Posts: 160

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

Joined: Nov 02, 2010
Posts: 87
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)

love demgracy, knowledge demogracy, open source and Java - OCPJP 76%
ankur trapasiya
Ranch Hand

Joined: Sep 24, 2010
Posts: 160

hey Imad...!!! Thanks a lot... my doubt got cleared and please dont mind putting similes in my lines is my habit .
I agree. Here's the link:
subject: SCJP : query regarding increment/decrement operator.
It's not a secret anymore!