File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes A few questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A few questions" Watch "A few questions" New topic
Author

A few questions

Tom Liu
Greenhorn

Joined: Feb 08, 2005
Posts: 4
Can anyone tell me the difference between i++ and ++i (assuming i has a value of 10)

Also, is it true that when a block is done, any local variables are "deleted"?

Finally, in a statement such as

while(array[i++]%2==0);

is i++ part executed without regard for the conditional statement, or will i++ be executed unless the equation returns true? Which comes first?

Thanks!
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Hello Tom,

My answer will sound cruel BUT try it!

Just write a quick bit of code....

int x = 10;
int y = 20;
x++;
++y;
System.out.println("x++ is: " +x + "++y is: " +y);

Try the same for the while statement.

(My lecturer David Marks would be so proud to hear me say his words!! BUT it works - TRY IT).

Remember though that the condition is checked before ++i
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

Question the first:

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.
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
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.
Tom Liu
Greenhorn

Joined: Feb 08, 2005
Posts: 4
Thanks for the replies! I got it all sorted out now
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A few questions