There is no difference between ++i and i++. It used to be that ++i was a little bit faster than i++, but I think that modern compilers optimize this out. However, there is a difference between a = i++ and a = ++i; In the former, a is assigned the value of i and then i is incremented (!-see comment). In the latter, i is incremented and then a is assigned the value of i.
!- note that the assignment happens after the incrementation; that is, the value of i is obtained (call it tempI), i is incremented, and then a is assigned the value of tempI. This means that the statment i = i++ actually accomplishes nothing!
Question the Second:
It depends, and Java says that you don't really need to know. What you do need to know is that these variables have gone out of scope and the compiler won't let you reference them outside the block. However [and this is advanced Java], I think that the compiled code retains these variables until the method concludes. Of course, all this is implementation specific; if you use a compiler other than javac, you may get different byte-code output.
Question the Third:
the conditional is evaluated before the ++, but the ++ still occurs, whether or not the conditional is true. (that is, the order of operations is: conditional expression is evaluated, ++ occurs, while statement operrates on the value of the conditional)
See for yourself!
Change the value of i from odd to even and see the resulting change in the behaviour.
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Joined: Oct 04, 2004
On your question about local variables being declared within a block being deleted you are sort of right.... They are not deleted as such, they just are not reachable from outside the block i.e. if you had a while or for loop and declared a varable locally within this block then they are not reachable outside these loops. That's ok if you want to use them within the block but if you wish to use them outside i.e. in more than one method then declare them at the top of the class.
Joined: Feb 08, 2005
Thanks for the replies! I got it all sorted out now