[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: programing styles
All local variables (and the entire operand stack) are allocated once at the
beginning of the method. No matter what code you write inside the method,
the JVM will not be allocating or deallocating local variables after this
initial allocation. By limiting scope of a local variable, you
let the garbage collection know that the reference is useless
from this point onwards and can be considered as garbage. Read how the JVM
allocates frames:
http://java.sun.com/docs/books/vmspec/2nd-edition/html/Overview.doc.html#172
57
Putting variable inside the loop may be better for space use as well
as it can then share space with other disjoint blocks.
For example, given the following code, its likely that the
compiler would map the variables i and j to the same local
variable on the stack, since their scopes do not overlap...
for (int i = 0; i < 10; i++) {
// use i...
}
for (int j = 10; j >= 0; j--) {
// use j...
}
Java allocates all local variables when a function is entered and cannot
allocate more during a method. Excluding the likely possibility that the
loop will be eliminated entirely by the JIT compiler, it may be easier
for the compiler in #1 to make the optimization of simply
using space on the operand stack instead of a local variable as
exemplified above.
In #2, though the variable's life is over as soon as the loop is done
as far as the programmer is concerned, the JVM doesn't know that and
keeps it in memory not allowing it to be garbage collected - note that
garbage collection is going to kick in only when needed - not for
every iteration in the loop.
But I would never make any such decision on which I thought was more
efficient. It should always be on the basis of readability. #1 would be
preferred if the scope of a was limited to inside the loop,
because limiting the scope of variables improves readability. The key
is a general principle of variable scoping. Always give a variable
the scope it needs and no more. Don't put it in an outer block if you
only intend it to be used inside an inner block.
Don't make it an instance variable if it is only intended to be used by
one method and does not need to be preserved from one invocation to
another.
The reason is that it limits the amount of code you need to review to
know that you have seen everything that can be affected by the variable.
Putting a variable inside a loop body says to a reader "This variable is
local to one iteration of this loop.".
Use javap and see how the assembler is going to execute
your method - you will be see that #1 is slightly more
efficient as well sometimes while #2 never beats #1.
Cheers,
Magesh
**************************************************
* The surest sign that intelligent life exists *
* elsewhere in the universe is the fact that it *
* has never tried to contact us. *
**************************************************
----- Original Message -----
From: "Mike Millson" <mgm@atsga.com>
To: <ajug-members@ajug.org>
Sent: Friday, November 01, 2002 3:13 PM
Subject: RE: programing styles
> Isn't #2 better for garbage collection, since you are not creating and
> destroying an object on each iteration of the loop? My understanding is
that
> the number of objects you create directly impacts speeds. The less
objects,
> the less garbage collection, and the faster the application. Given that, I
> would think #2 is much more efficient and the style to use.
>
> Mike
>
> -----Original Message-----
> From: Magesh Umasankar [mailto:umagesh@apache.org]
> Sent: Friday, November 01, 2002 2:42 PM
> To: ajug-members@ajug.org
> Subject: Re: programing styles
>
>
> I would say the first one is - keep variable
> declarations as local as possible - better
> for garbage collection also, in most cases.
>
> In your case, you gain nothing by declaring
> them outside the for loop as you don't seem
> to be using them outside the loop.
>
> Cheers,
> Magesh
>
> **************************************************
> * The surest sign that intelligent life exists *
> * elsewhere in the universe is the fact that it *
> * has never tried to contact us. *
> **************************************************
> ----- Original Message -----
> From: "Gudavalli, Manidhar" <manidhar.gudavalli@eds.com>
> To: <ajug-members@ajug.org>
> Sent: Friday, November 01, 2002 2:34 PM
> Subject: programing styles
>
>
> >
> >
> >
> > HI
> >
> > Which style of coding is best ?
> >
> > [1]
> > private void myproc(Vector vect){
> > for (i=1; i < vect.size() ; i++){
> >
> > MyObject obj = (MyObject) vect.elementAt(i);
> > int y = obj.getY();
> > }
> > }
> >
> > or
> > [2]
> > private void myproc(Vector vect){
> > MyObject obj = null;
> > int y= 0;
> >
> > for (i=1; i < vect.size() ; i++){
> >
> > obj = (MyObject) vect.elementAt(i);
> > y = obj.getY();
> > }
> > }
> >
>
>