Loop != Loop  
Author Message
Gerbrand van Dieijen





PostPosted: 2005-1-5 17:06:00 Top

java-programmer, Loop != Loop Stephen Riehm schreef:
>
> don't those two loops have different semantics, ie: the scope of myvalue?
> My understanding would be that myvalue in the first loop would be
> overwritten 9 times and then the last value would be available to code
> appearing after the loop. The myvalue in the second loop basically has
> no effect because it falls out of scope (and into the garbage
> collector's hands) as soon as you leave the loop.
>

Oh, it was just a quick example, there supposed to be done something
with myvalue later in the loop. The point was, myvalue is only needed
inside the loop and not outsite.

int myvalue=0;
for (int i=0;i<10;i++) {
myvalue=calculate(i);
.. do something with myvalue
}
.. myvalue no longer needed

for (int i=0;i<10;i++) {
int myvalue=calculate(i);
.. do something with myvalue
}
..my value not needed anymore

I believe this example is somewhere on the web too, but I forgot where
(probably javasoft.com or javaworld.com).
 
Grant Wagner





PostPosted: 2005-1-13 1:41:00 Top

java-programmer >> Loop != Loop "TechBookReport" <email***@***.com> wrote in message
news:crge1r$6l4$email***@***.com...
> Stephen Riehm wrote:
> > Gerbrand van Dieijen wrote:
> >
> >> Similar idea is, how to use a loop:
> >> When I was less experienced in programming, I use to write
> >> int myvalue=0;
> >> for (int i=0;i<10;i++) {
> >> myvalue=calculate(i);
> >> }
> >> however, now I write:
> >> for (int i=0;i<10;i++) {
> >> int myvalue=calculate(i);
> >> }
> >
> >
> > don't those two loops have different semantics, ie: the scope of
myvalue?
> > My understanding would be that myvalue in the first loop would be
> > overwritten 9 times and then the last value would be available to
code
> > appearing after the loop. The myvalue in the second loop basically
has
> > no effect because it falls out of scope (and into the garbage
> > collector's hands) as soon as you leave the loop.
> >
> > or am I missing something?
> >
> > Steve
> > (just getting to grips with java)
>
> Nope, you're right about the scoping. And best practice is to limit
> scope as much as possible.

Right about scoping, but wrong (at least as I understand it) about
eligibility for garbage collection. -myvalue- won't be eligible for
garbage collection until the method exits, because a slot was reserved
for it when the method started. It goes out of scope (visibility to the
remaining source code) but the VM doesn't "lose track of it" until the
method exits.

--
Grant Wagner <email***@***.com>


 
Ann





PostPosted: 2005-1-13 2:49:00 Top

java-programmer >> Loop != Loop
"Grant Wagner" <email***@***.com> wrote in message
news:pidFd.79$email***@***.com...
> "TechBookReport" <email***@***.com> wrote in message
> news:crge1r$6l4$email***@***.com...
> > Stephen Riehm wrote:
> > > Gerbrand van Dieijen wrote:
> > >
> > >> Similar idea is, how to use a loop:
> > >> When I was less experienced in programming, I use to write
> > >> int myvalue=0;
> > >> for (int i=0;i<10;i++) {
> > >> myvalue=calculate(i);
> > >> }
> > >> however, now I write:
> > >> for (int i=0;i<10;i++) {
> > >> int myvalue=calculate(i);
> > >> }
> > >
> > >
> > > don't those two loops have different semantics, ie: the scope of
> myvalue?
> > > My understanding would be that myvalue in the first loop would be
> > > overwritten 9 times and then the last value would be available to
> code
> > > appearing after the loop. The myvalue in the second loop basically
> has
> > > no effect because it falls out of scope (and into the garbage
> > > collector's hands) as soon as you leave the loop.
> > >
> > > or am I missing something?
> > >
> > > Steve
> > > (just getting to grips with java)
> >
> > Nope, you're right about the scoping. And best practice is to limit
> > scope as much as possible.
>
> Right about scoping, but wrong (at least as I understand it) about
> eligibility for garbage collection. -myvalue- won't be eligible for
> garbage collection <insert>

GC only collects objects, primitives like int are not objects.

</insert>until the method exits, because a slot was reserved
> for it when the method started. It goes out of scope (visibility to the
> remaining source code) but the VM doesn't "lose track of it" until the
> method exits.
>
> --
> Grant Wagner <email***@***.com>
>
>