Will Java create a new object every time a foreach loop is introduced? I'm not talking about every iteration, but if you have a foreach loop that is used multiple times, is it created every time?
A simple example:
for(Object o : Objects)
{
for(Object p : Objects2)
{
}
}
Could there be only one pper run or create pfor each Object o? Does the garbage collector need to return an object from the foreach loop every time it exits?
In particular, I am writing Android code based on Android. It will iterate over all game objects with a given speed per second or as fast as it can, depending on the circumstances.
This may be a case of premature optimization, but if using an explicit loop foror whilecan guarantee that I will not have excessive garbage collection from my loops, then I can set this as the coding standard for the project.
More specific:
public void update()
{
for(GameObject gObj : gameObjects)
{
gObj.update();
}
}
C update()is called from a thread designed to make calls based on the time I described earlier.
Update:
I ask if a new pone is being created for everyone oat Objects. Not if it copies objects to Objects2. Also, VM need to create a new Reference p, and then assemble this link between iterations of the outter loop? And, more specifically, in my case, does it collect a link between method calls?
Update:
.
?
for(int i = 0; i < 1000; i++)
{
for(Object o : Objects)
{
o.update();
}
}
.
Iterator<Object> iter;
for(int i = 0; i < 1000; i++)
{
iter = Objects.iterator();
while(iter.hasNext());
{
iter.getNext().update();
}
}
Update:
:
import java.util.ArrayList;
import java.util.Iterator;
public class TestLoops
{
public static Iterator<Object> it;
public static ArrayList<Object> objects;
public static void main(String... args)
{
objects = new ArrayList<Object>();
it = objects.iterator();
it = objects.iterator();
Iterator<Object> newIt1 = objects.iterator();
Iterator<Object> newIt2 = objects.iterator();
}
}
-:
public class TestLoops {
public static java.util.Iterator<java.lang.Object> it;
public static java.util.ArrayList<java.lang.Object> objects;
public TestLoops();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String...);
Code:
0: new #2 // class java/util/ArrayList
3: dup
4: invokespecial #3 // Method java/util/ArrayList."<init>":()V
7: putstatic #4 // Field objects:Ljava/util/ArrayList;
10: getstatic #4 // Field objects:Ljava/util/ArrayList;
13: invokevirtual #5 // Method java/util/ArrayList.iterator:()Ljava/util/Iterator;
16: putstatic #6 // Field it:Ljava/util/Iterator;
19: getstatic #4 // Field objects:Ljava/util/ArrayList;
22: invokevirtual #5 // Method java/util/ArrayList.iterator:()Ljava/util/Iterator;
25: putstatic #6 // Field it:Ljava/util/Iterator;
28: getstatic #4 // Field objects:Ljava/util/ArrayList;
31: invokevirtual #5 // Method java/util/ArrayList.iterator:()Ljava/util/Iterator;
34: astore_1
35: getstatic #4 // Field objects:Ljava/util/ArrayList;
38: invokevirtual #5 // Method java/util/ArrayList.iterator:()Ljava/util/Iterator;
41: astore_2
42: return
}