aspose file tools*
The moose likes Java in General and the fly likes object instantiation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "object instantiation" Watch "object instantiation" New topic
Author

object instantiation

Joseph Miller
Greenhorn

Joined: Jun 19, 2001
Posts: 21
in this pseudo-example, am I correct in thinking that #2 is more favorable because less objects are instantiated and placed on the stack?
1.
for (int i=0; i<1000; i++)
{
Object a = otherObject(i);
... processing ...
}
2.
Object a = null;
for (int i=0; i<1000; i++)
{
a = otherObject(i);
... processing ...
}
thnx.
Joe Miller
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
i'd have to agree with u.
less objects (or refrences to objects) are being created in eample 2, this its better than 1 .
Alex Ku
Ranch Hand

Joined: Jan 15, 2002
Posts: 47
Hi,
Just have something to share
I had a code like this before

If the file is to large, eventually this code will generate a exception. I don't remember what exception is. But once I put the declaration outside the loop, no more error.
Kawaii
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: object instantiation