File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes What is the dfference between these two loops? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the dfference between these two loops?" Watch "What is the dfference between these two loops?" New topic
Author

What is the dfference between these two loops?

Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

Loop 1

Loop 2


Why the Loop1 is preferred over the second one? Is it because of the new reference created everytime? Does it has some performance issues?


My Blog SCJP 5 SCWCD 5
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
Balagopal Kannampallil
Ranch Hand

Joined: Oct 18, 2004
Posts: 136

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.


SCJP 5.0 and now fighting with ExtJS
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

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.

Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Hello Himanshu,

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.
Ravikanth kolli
Ranch Hand

Joined: Feb 10, 2008
Posts: 179

well i dont think there is not going to be any difference in the performance with the 2 loops. I consider it to be for more about readability that people prefer the first method.


-kolli
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

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....

Thanks again everyone for your replies.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

It might be considered a defect in C++, but in Java, ....... order in which they're first mentioned changes.


Thanks for your reply Ernest.
I got something which states that Loop1 style is more efficient as compared to Loop2. But the reason is not given there and I am also not able to find out. src

Tom Johnson
Ranch Hand

Joined: May 11, 2005
Posts: 142
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


<a href="http://faq.javaranch.com/java/UseCodeTags" target="_blank" rel="nofollow">Use Code Tags!!</a>
Marky Vasconcellos
Ranch Hand

Joined: Jan 28, 2009
Posts: 36
It's work the same as:



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
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

Marky Vasconcellos wrote:It's work the same as:


You are right Marky but the question is what makes Loop1 more efficient?
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3012
    
  10
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.
Marky Vasconcellos
Ranch Hand

Joined: Jan 28, 2009
Posts: 36
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the dfference between these two loops?