• 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

Variable declaration inside loops - what happens?

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

A trivial question I know. I did some searching and a lot of c++ discussion came back about whether to do it or not (the consensus was to declare inside a loop for scope) but I'm interested in knowing what exactly happens? For each iteration is a new myVar created, or does Java know that int myVar is still the same myVar it was the previous iteration?
 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it'll just execute that loop over and over again...
kind of pointless

so when i is 2, myVar will equal 2, but then it will get re-initialized back to 1 when the loop restarts, you need to make the variable have
"method" scope, or "class" scope to keep actual track of the 1 and two...

but at the very end if you were to print myVar in the loop, at 99, it
would = 1;

i cant remember if you put <=100 in that case it would print 2 at the end.

Justin
 
Nathan Leniz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Justin,

I guess my question is since it's on the stack, is it ever lost? Can't think of a better term to call it. I can visualize it being created initially, but does Java go back to that same exact spot?

The code actually does nothing, I just wanted to help visualize my question.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think what you're after is ... When declared outside the loop the JVM allocates the variable once on the stack. When declared inside the loop the JVM might allocate it on the stack every time through the loop and de-allocate at the end. You'll have to get one of the Byte-Code gurus around here to tell whether the compiler writes it that way, or whether it might be optimized out at or run time.

I haven't looked into it because I don't care. And that's really good news to me. I believe Java is fast enough that I can write code that communicates my intent - using scope to show that the variable has no meaning outside the loop - without worrying about a couple instructions here or there.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I remember correctly from earlier discussions on this topic, declaring the local variable inside or outside the loop creates absolutely identical byte code.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tend to agree that any differences in the byte code generated - and there may well be no differences - are not very important or interesting in 99.99% of applications.

There is an important difference between declaring inside or outside the loop. Java compiler insists that all variables must definitely have been assigned a value, before their value is first used. If the variable is declared outside the loop, then just one assignment, outside the loop, would be enough. If the variable is declared inside the loop, then you'd need to ensure it gets assigned a value each time through the loop. If that is your intent, then it would be better to write the code that way, to help the Java compiler to spot any mistakes.
 
reply
    Bookmark Topic Watch Topic
  • New Topic