• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What happens?

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any difference in this code? If there is an SomeObject array called array...
SomeObject object = null;
for (int i = 0; i < array.length; i++) {
object = array[i];
...some processing...
}
or........
for (int i = 0; i < array.length; i++) {
SomeObject object = array[i];
...some processing...
}
 
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Aaron Webb:
Is there any difference in this code? If there is an SomeObject array called array...
SomeObject object = null;
for (int i = 0; i < array.length; i++) {
object = array[i];
...some processing...
}
or........
for (int i = 0; i < array.length; i++) {
SomeObject object = array[i];
...some processing...
}


I think no. 2 is wrong, you have to declare before the loop.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Either form is acceptable. The second form creates a bunch of unneeded pointers but since they are on the stack and not the heap, it isn't a big deal.
 
Aaron Webb
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
both are correct and work fine i just didn't know if there was a performance difference one way or the other
 
Aaron Webb
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cool thats what i was wondering...thanks
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Aaron Webb:
both are correct and work fine i just didn't know if there was a performance difference one way or the other


Well, did you try to benchmark the two? That would give you the only authentic answer...
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you benchMark?
Is it Just a way of checking how much time each loop takes.Do you do it using time functions.?
Or is there someother method to do this?
Thanks.
 
reply
    Bookmark Topic Watch Topic
  • New Topic