I haven't tested this, but in my opinion it makes no difference, and
someone says that at least in Sun's JVM
it doesn't...
Someone said that, in the second case, you are creating a new object on
each iteration. That's not
true. Syntactically you're just declaring the variable on each
iteration, but in practice I think nothing
happens. These are local variables and exist in the stack. When the
method enters, the stack pointer is moved whatever number of bytes
necessary for all the local variables. That's the simplest way... maybe
the
stack pointer is corrected each time you enter a local block which
declares even more variables. That would
save a few bytes, but they would only be important on highly recursive
functions where you run the risk of
getting a stack overflow and a few bytes saved on each recursion might
make a difference.
But, in either case, declaring one or one hundred local variables would
normally make no
difference -- you just adjust the stack pointer once, when you enter the
block and once when you exit it.
At least that's what I've seen in all the architectures and languages
(like C) that allow these kinds
of constructions. I have been programmerd in Java for years but not
studied much its internal architecture
and bytecode. So much to learn, so little time!!!
Barzilai
David Gallardo wrote:
> --- "Stacy C. May" <[EMAIL PROTECTED]> wrote:
> > Is there a difference in JVM overhead/expense the
> > following code? and why?
> >
> > public void someMethod(List passedList) {
> >
> > Iterator iter = passedList.iterator();
> >
> > while (iter.hasNext()) {
> >
> > MyObject myObject = (MyObject)iter.next();
> >
> > // do some processing on myObject
> > }
> > }
> >
> > compared to:
> >
> > public void someMethod(List passedList) {
> >
> > Iterator iter = passedList.iterator();
> > MyObject myObject = null;
> >
> > while (iter.hasNext()) {
> >
> > myObject = (MyObject)iter.next();
> >
> > // do some processing on myObject
> > }
> > }
> >
> > Thanks in advance.
> > N�!jxʋ���z�!��)�*'���z�h��i��0�̬r�ܢo�j���+-q�
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! - Official partner of 2002 FIFA World Cup
> http://fifaworldcup.yahoo.com
>
> To change your membership options, refer to:
> http://www.sys-con.com/java/list.cfm
To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm