File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What happens?

 
Aaron Webb
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • 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...
}
 
John Lee
Ranch Hand
Posts: 2545
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cool thats what i was wondering...thanks
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • 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...
 
Divya Venkatesh
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic