Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
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 Barcodes with iOS this week in the iOS forum
or Core Java for the Impatient in the Java 8 forum!

JavaRanch » Java Forums » Java » Java in General
Bookmark "object instantiation" Watch "object instantiation" New topic

object instantiation

Joseph Miller

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?
for (int i=0; i<1000; i++)
Object a = otherObject(i);
... processing ...
Object a = null;
for (int i=0; i<1000; i++)
a = otherObject(i);
... processing ...
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
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.
I agree. Here's the link:
subject: object instantiation