• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

stack in loops

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everybody! this is my first message.

I've been studying for SCJP for a few months, I have ~ 10 years of experience in Java but now i want to achieve this certification.

I have one stack-related question:

What if I declare a variable in a loop body? It creates an instance in every iteration of loop? (Note, I say variable, not object):


Well, how many i's I have? Yes, I have one. But something I remember says to me that this is not very well practice (I remember C++, I think in this language
this will create an instance of i every iteration!!). Then, this is my explanation: I have only one i, but the stack is filling with int's that nobody never will use, isn't it?

PD: Excuse my bad english, feel free for correct me!! I expect I explained this not very bad but...

Thank you, very much!!!
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have only one i, but the stack is filling with int's that nobody never will use, isn't it?



I dont see any stack. Your local variable will simple be out of scope and will be garbage collected. A stack is involved only when a method parameter, say, contains a variable. The state of a method call will then have to be pushed into a stack

If variable 'i' is a constant of sorts or if its state when it enters the loop does not matter, you can declare variable 'i' outside the said loop.
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
PS: Welcome to javaranch Pablo
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pablo Marmol wrote:

Well, how many i's I have?



Logically to me it looks like for every loop iteration new i (variable) is created and as soon as that iteration finish, i (variable) become out of scope. If you were asking how many i (variable) is created? one for each iteration on stack, but it is not like one on top of another. It is like one created then popped out then new created.

push(i) for first iteration
pop() as first iteration finish
push(i) for second iteration
..
..
and so on

I am not sure I answered your question but tried to put some thought to clear your doubt, If I did.
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am confused for the solution for your problem.
So wanna say
Welcome to Javanranch only.
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
whoops, just realized you are talking about a local variable whose value is indeed maintained on the stack. For some reason I thought you were referring to a member variable.

I have only one i, but the stack is filling with int's that nobody never will use, isn't it?



Do not worry about it. They will be garbage collected. Java will take care of it.
 
Pablo Marmol
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pradeep Kr wrote:

Pablo Marmol wrote:

Well, how many i's I have?



Logically to me it looks like for every loop iteration new i (variable) is created and as soon as that iteration finish, i (variable) become out of scope. If you were asking how many i (variable) is created? one for each iteration on stack, but it is not like one on top of another. It is like one created then popped out then new created.

push(i) for first iteration
pop() as first iteration finish
push(i) for second iteration
..
..
and so on

I am not sure I answered your question but tried to put some thought to clear your doubt, If I did.



Ok. Understood.

The "push, pop, push, pop, ..." was the key for me to understand that. But that don't seem to be exactly like you say (please excuse me, the explanation was perfect in a conceptual sense): I have write code declaring variable i within loop, and declaring variable i out of loop, and those 2 programs take the same time (aprox). For example, a loop with 1e8 iterations take the same time declaring variables within loop and declaring out. I think there is some optimization when you declare variables within loops. I will drop an eye to see the JLS.

Thanks to everybody, thanks for the reply and thanks for my welcome to the ranch!!!

PD: If you want, I'll post the 2 programs, i haven't do because they are trivial for programmers like us!!!
 
catch it before it slithers away! Oh wait, it's a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic