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 Beginning Java and the fly likes What happens? 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 » Beginning Java
Bookmark "What happens?" Watch "What happens?" New topic
Author

What happens?

Aaron Webb
Greenhorn

Joined: Aug 16, 2001
Posts: 17
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

Joined: Aug 05, 2001
Posts: 2545
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

Joined: May 05, 2000
Posts: 13974
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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Aaron Webb
Greenhorn

Joined: Aug 16, 2001
Posts: 17
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

Joined: Aug 16, 2001
Posts: 17
cool thats what i was wondering...thanks
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Divya Venkatesh
Greenhorn

Joined: Nov 05, 2002
Posts: 28
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.


Divya
 
Consider Paul's rocket mass heater.
 
subject: What happens?
 
Similar Threads
array sort
Vector and ArrayList Behavior got me in a loop...
The return statement question
Check an array for equality
Casting