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 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 Murach's Java Servlets and JSP this week in the Servlets 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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: object instantiation
 
Similar Threads
Interface and array
Strange java.lang.OutOfMemoryError
memory leaks
isOverlap method problems
What happens?