Actually, it does NOT allocate a new *instance*. It sets up a variable 
slot that can hold a reference to an instance, but the intial value of 
that variable is null, so no heap allocation occurs.

John Ghidiu wrote:

> Stacy,
> I would think that there would be, as the first example declares a new 
> instance of MyObject every iteration, which I believe will allocate 
> memory on every pass, whereas the second example will not. However, I 
> cannot say for certain that the compiler will optimize this. Perhaps a 
> simple benchmark would be the best solution?
>
> Regards,
> John
>
> John Ghidiu
> Benderson Development Company Inc.
> [EMAIL PROTECTED]
> (716) 878-9376
>
>     -----Original Message-----
>     *From:* Stacy C. May [mailto:[EMAIL PROTECTED]]
>     *Sent:* Friday, June 21, 2002 13:39
>     *To:* JDJList
>     *Subject:* [jdjlist] JVM Overhead
>
>     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.
>     Nj�^(TM)zS(��z+�i��0�̬r0/00ܢo�j��-+-
>
> 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

Reply via email to