Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

object instantiation

 
Joseph Miller
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic