• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

instance in a for loop - right /wrong/doesn't matter ?

 
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Out of curiousity:

What is the diff (if any) between this:

for (int x=0; x<1000; x++)
{
boolean flag=obj[x].getInfo();
}

and this:

boolean flag;
for (int x=0; x<1000; x++)
{
flag=obj[x].getInfo();
}

Thanks for any help


 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regarding performance, none. It should compile to exactly the same byte code, as far as I know.
 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what about memory usage?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Local variables are stored in machine registers. As Ilja says, these will compile to identical code.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even if the local variable is on the stack instead of in a register (I'm not familiar with the JVM enough, but I assume it works the same as C/C++), variables in a loop are reused so there's really only one variable called flag.

The only difference here is that flag isn't available outside of the first loop. That being said, I strive to make variables have the smallest scope possible as it avoids name collisions and makes it clear that the variable isn't used outside the loop without having to read the rest of the method.
 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so basically it is better to user the first option:
for (int x=0; x<1000; x++)
{
boolean flag=obj[x].getInfo();
}

because:
a. once it's out the loop the stack is free
b. collisions (can't use it outside the loop)

agree?
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agreed. Generally, your first order priority should be to express the intentions of the code as well as possible. Let the compiler care about optimizing execution - the modern ones are quite good at it. And once you really encounter a performance problem, you can work on it, with the knowledge that you are working on a real, concrete problem, and that your code is easy to understand and change - and therefore also easy to performance optimize.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Peter Primrose:
a. once it's out the loop the stack is free

Agreed on all but this. Again, extrapolating from C/C++, the stack is only unrolled when the method returns -- not partially during the method execution. It's certainly possible to partially unroll the stack after the loop, but I doubt the JVM does this (and you wouldn't count on it anyway since it's not part of the specification) since it adds complexity for little gain.

I do believe, however, that if you had another loop with a boolean variable inside it, the JVM would reuse the same space in the stack for both variables since they aren't used at the same time. Again, this is another optimization left up to the compiler or JVM.

Ilja's advice takes precedence above all that: write clear code that expresses your intentions first.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
David, you are right. Of course, depending on the architecture the program runs on, it might not make use of the stack at all - it might just use a register in this case, for example.
 
reply
    Bookmark Topic Watch Topic
  • New Topic