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 old for loop vs new for loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "old for loop vs new for loop" Watch "old for loop vs new for loop" New topic
Author

old for loop vs new for loop

pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
what is the main diffrence here?

can anyone explain with a simple example?


regards ,
prasanna.s.k
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

The main difference is easier readability, at the cost of losing the iterator or array index.

Observe:

versus

So, when should you use the new loop? The answer: whenever you feel like it, and do not need the array index or iterator. I'm already using it in 95% of the cases.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
It basically hides the iterator.

This example doesn't display the space saving it can sometimes buy you, but it will become apparent if you start using it.


[ November 20, 2007: Message edited by: Bill Shirley ]

Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
thanks to the wonderfull and rocking replies.


it really helped


cheers,
Prasanna.s.k

Wish i could give a party for that!!!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
. . . but remember the for-each loop ("new for loop") is read-only. You can get data from the individual members of the array, but you can't writeCR
pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
how can you write using the old for loop style? can you give a simple example







cheers
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
You can usually find the answer to that sort of question in the Java Tutorial.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: old for loop vs new for loop
 
Similar Threads
Codes For Practice
for and while loops
WA #2 ..... word association
while loop
WA #1.....word association