• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object creation in a loop

 
chandana sapparapu
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please look at the following code snippets
------------------------------------
InLoop
for(){
String a = new String();
}
-------------------------------------
Outsideloop

String a = new String();
for(){
a= new String();
}
--------------------------------------

Is the second one better compared to the first one. If a were a primitive variable, I agree that InLoop is a better way to code. I am not sure about String declaration outside the loop, memorywise or even otherwise.

Thanks,
Chandana
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say it depends on what you want to do with it. If you declare a variable within a block it's only visible in that block. I would also say that the outsideloop is better performance wise because you only declare the variable once rather than repeatedly, and each declaration consumes overhead.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, the Java compiler will generate identical code for these two snippets. There's precisely no difference at all between them performance-wise (except that in the out-of-loop case, the String may be retained longer before it can be garbage collected.) In particular, just declaring a variable causes absolutely no code to be generated.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
There's precisely no difference at all between them performance-wise


And that is because memory for *all* local variables is allocated on the stack when a method is entered - regardless of their actual scope.

(except that in the out-of-loop case, the String may be retained longer before it can be garbage collected.)


My current understanding is that this isn't true. Local reference variable are only destroyed when exiting the method, and therefore referenced objects aren't garbage collected beforehand (unless you make the reference point to something else, of course).
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
(unless you make the reference point to something else, of course).


Indeed, and the compiler does reuse local variables. Note that I said the in-the-loop one may be collected sooner. The case in which it can be is when the local variable used for the in-loop Java variable is reused for another purpose after the loop.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My current understanding is that this isn't true. Local reference variable are only destroyed when exiting the method, and therefore referenced objects aren't garbage collected beforehand (unless you make the reference point to something else, of course).

I had quite a long discussion over this point several years ago when I used similar code in a mock exam question. Writing some test code revealed that local objects could be GCed before the method exited if there was no way that the code using those objects would be executed.
Bill
[ October 21, 2004: Message edited by: William Brogden ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
Indeed, and the compiler does reuse local variables.


Good point - I didn't think about that. Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!