0

Is it

Object o;
for (...) {
  o = new Object();
...
}

really better than

for (...) {
Object o = new Object();
...
}

in terms of performance?

I read this on a computer science book some time ago, but I was wondering how the modern compilers (especially the java compiler) react to this two piece of codes. Are the first form optimized in some way to have the same performances of the second one?

tigerjack
  • 1,158
  • 3
  • 21
  • 39
  • 1
    Contrary to your title, neither piece of code creates a new object outside the loop. It's important to understand the difference between objects and variables. – Jon Skeet May 22 '15 at 16:09
  • Well, yes, of course. I think I was just typing too fast, but thanks for point it out. – tigerjack May 22 '15 at 16:31

0 Answers0