Funny you should ask!  I just wrote a small program to test the same thing a couple of weeks ago.
 
I can honestly say that on JDK 1.3 it makes no difference.  I think that the compiler optimizes the code either way.
However, IMHO, the second one is more clear to me as a programmer.  I can see that myObject is the object you're going to instantiate/work on for every member of the loop. 
 
Greg
 

[Greg Nudelman]  -----Original Message-----
From: Stacy C. May [mailto:[EMAIL PROTECTED]]
Sent: Friday, June 21, 2002 10:39 AM
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.
N‡!jx�‹�(tm)�›z�!Šš)�*'��Ÿz�h†�i��0�̬r‰ܢo�j��-+-
To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm

Reply via email to