This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
For me, the difference between those two loops is the scope of the object. I prefer the second loop because it keeps the scope of the object as small as possible, which I like.
As for performance, I don't know, I usually don't work on systems where this level of performance optimization would be meaningful (not in years anyway). I would think that with compilers being as smart as they are now, the performance would be about the same as they are allowed to re-arrange declarations and assignments to optimize speed. But perhaps someone else has better experience than I do.
Balagopal Kannampallil wrote:In the second loop the newly created objects inside the loop will take more time to get Garbage collected as the number of references are more when compared to the first loop.
Well we are adding those Objects in arrayList so I dont think that GC will come into picture because in both of these loops arrayList visibility is same.
Here is the quote from EFH that I have copied from my old post
EFH wrote:a reference is just a reserved spot on the stack, and absolutely nothing is done at runtime to "create" one. Therefore this loop has an empty body; the Hotspot compiler is likely to optimize it away completely.
A reference has any importance when an object is associated to it. So in your case scope is the only thing that differs in given two loops. And it's already been pointed out by other people.
This should surely clear the confusion.
Thanks everyone for your replies. In my code review i coded using the Loop2 style.... and it came back as a defect..... so i thought that there may b some difference which I dont know..... so I posted it here....
Himanshu Gupta wrote:Thanks everyone for your replies. In my code review i coded using the Loop2 style.... and it came back as a defect....
It might be considered a defect in C++, but in Java, it honestly makes no difference at all. The compiler will generate the exact same bytecode, in fact, with one small difference: in version 1, obj, arr, and i will be in registers 1, 2, and 3, respectively, while in version 2 they'll be in 3, 1, and 2 -- just because the order in which they're first mentioned changes.
My 2 cents is that I prefer to declare the reference inside the loop, then its clear that its only used inside the loop. If its declared outside, in 6 months time you might wonder is that supposed to be used somewhere outside the loop.
I think in any meaningful application, these micro-optimizations are far less important than the efficiency of database access, remote method calls, webservice calls etc
Each of their nuggets of wisdom contracted to a sound bite: Joshua Bloch: Write Lots of Code; Chet Haase: Don't Put Your Entire Application in One Method; Masood Mortazavi: Start Simple and Keep Learning; Cay Horstmann: First, Don't Panic
Himanshu Gupta wrote:You are right Marky but the question is what makes Loop1 more efficient?
Nothing does - it's not more efficient. The author of JAC_052 is, how can I put this gently? Full of crap. Ignore it.
Joined: Jan 28, 2009
Sorry.. i post that to say.. the three are the same.. no one object will be have reference to null cause they are all in the ArrayList.. the only difference is the size of the final compiled class.. but some bytes less or higher, and no significant.