This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Performance and the fly likes collection.size() vs. array.length Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "collection.size() vs. array.length" Watch "collection.size() vs. array.length" New topic
Author

collection.size() vs. array.length

paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20488
    ∞

In the article at http://java.oreilly.com/news/javaperf_0900.html the advantages of not having collection.size() used in the for loop are pointed out. Would this apply equally to array.length?


permaculture Wood Burning Stoves 2.0 - 4-DVD set
Jack Shirazi
Author
Ranch Hand

Joined: Oct 26, 2000
Posts: 96
No. Java arrays (e.g. char[], Object[], not collection objects) cannot change size. So the value is a constant for any one array, and the loop has a constant value to compare against. The problem with collection.size() is that it makes a method call for every iteration of the loop.
 
Consider Paul's rocket mass heater.
 
subject: collection.size() vs. array.length
 
Similar Threads
Array list > for loop or iterator
casting optimization
Top Ten JSP Tips
future of java technology
eliminating method call makes loop slower