Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9225023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:28:32+00:00 2026-06-18T04:28:32+00:00

Will Java create a new object each time a foreach loop is entered? I’m

  • 0

Will Java create a new object each time a foreach loop is entered?
I’m not talking about each iteration, but if you have a foreach loop that is used multiple times, is it creating objects each time?

Simple Example:

for(Object o : Objects)
{
    for(Object p : Objects2)
    {
    }
}

Would there only be one p per execution, or would p be instantiated for each Object o?
Does the Garbage Collector have to recover an object from the foreach loop each time it is exited?

More specifically, I’m writing some Android based game code. It will iterate over all the game objects at a given rate per second, or as fast as it can depending on the circumstances.

It may be a case of premature optimization, but if using an explicit for or while loop can guarantee me that I won’t have excess garbage collection from my loops, then I can establish that as a coding standard for the project.

More specifically:

public void update()
{
    for(GameObject gObj : gameObjects)
    {
        gObj.update();
    }
}

With update() being called from a thread designed to make the calls based on the timing I described before.

Update:
I am asking if there is a new Reference p being created for each o in Objects. Not if it copies the objects in Objects2. So does the VM have to create a new Reference p, and then does it collect that reference between iterations of the outter loop? And more specifically in my case, does it collect the reference between method calls?

Update:
From comments on Matt Ball’s answer.
Which would create less Garbage Collection work?

//Loop just to have it run a number of times
//Could be running the inner foreach numerous time for any reason
for(int i = 0; i < 1000; i++)
{
    for(Object o : Objects)
    {
        o.update();
    }
}

vs.

Iterator<Object> iter;
//Loop just to have it run a number of times
//Could be running the inner foreach numerous time for any reason
for(int i = 0; i < 1000; i++)
{
    iter = Objects.iterator();

    while(iter.hasNext());
    {
        iter.getNext().update();
    }
}

Update:
Comparing scopes:

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();

            //Every time we start a foreach loop, does it creates a new reference?
        Iterator<Object> newIt1 = objects.iterator();
        Iterator<Object> newIt2 = objects.iterator();       

    }
}

Produces this Bytecode:

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
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:28:33+00:00Added an answer on June 18, 2026 at 4:28 am

    There is no magical object construction that happens with a for-each loop. This syntax:

    for(Object o : Objects)
    {
        for(Object p : Objects2)
        {
        }
    }
    

    is only shorthand for this:

    for(Iterator<Object> iter = Objects.iterator(); iter.hasNext();)
    {
        Object o = iter.next();
        for(Iterator<Object> iter2 = Objects2.iterator(); iter2.hasNext();)
        {
            Object p = iter2.next();
        }
    }
    

    If would there be an iter2 reference created for every object I had in Objects?

    That depends on where Objects2 comes from. Does it come from o? It also depends on how Objects2.iterator() is implemented. Iterable#iterator() is expected to return independent iterators, which means that Objects2.iterator() would almost certainly return a new Iterator for each invocation. But that does not say anything about whether or not iterating through the objects in Objects2 (using iter2) creates other new objects.

    This sounds like premature optimization, overall.


    Which would create less Garbage Collection work?

    Neither. The enhanced for loop (aka for-each loop) is simply syntactic sugar. The bytecode produced by the compiler is identical. Complete example:

    package com.stackoverflow;
    
    import java.util.Iterator;
    
    public class Question14640184
    {
        public void enhancedForLoop(Iterable<Object> objects1, Iterable<Object> objects2)
        {
            for(Object o1 : objects1)
            {
                for(Object o2 : objects2)
                {
                    // do something
                }
            }
        }
    
        public void iteratorForLoop(Iterable<Object> objects1, Iterable<Object> objects2)
        {
            for(Iterator<Object> iter1 = objects1.iterator(); iter1.hasNext();)
            {
                Object o1 = iter1.next();
                for(Iterator<Object> iter2 = objects2.iterator(); iter2.hasNext();)
                {
                    Object o2 = iter2.next();
                }
            }
        }
    }
    

    Compile:

    ✗ javac Question14640184.java
    ✗ javap -c Question14640184
    Compiled from "Question14640184.java"
    public class com.stackoverflow.Question14640184 extends java.lang.Object{
    public com.stackoverflow.Question14640184();
      Code:
       0:   aload_0
       1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
       4:   return
    
    public void enhancedForLoop(java.lang.Iterable, java.lang.Iterable);
      Code:
       0:   aload_1
       1:   invokeinterface #2,  1; //InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
       6:   astore_3
       7:   aload_3
       8:   invokeinterface #3,  1; //InterfaceMethod java/util/Iterator.hasNext:()Z
       13:  ifeq    57
       16:  aload_3
       17:  invokeinterface #4,  1; //InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
       22:  astore  4
       24:  aload_2
       25:  invokeinterface #2,  1; //InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
       30:  astore  5
       32:  aload   5
       34:  invokeinterface #3,  1; //InterfaceMethod java/util/Iterator.hasNext:()Z
       39:  ifeq    54
       42:  aload   5
       44:  invokeinterface #4,  1; //InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
       49:  astore  6
       51:  goto    32
       54:  goto    7
       57:  return
    
    public void iteratorForLoop(java.lang.Iterable, java.lang.Iterable);
      Code:
       0:   aload_1
       1:   invokeinterface #2,  1; //InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
       6:   astore_3
       7:   aload_3
       8:   invokeinterface #3,  1; //InterfaceMethod java/util/Iterator.hasNext:()Z
       13:  ifeq    57
       16:  aload_3
       17:  invokeinterface #4,  1; //InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
       22:  astore  4
       24:  aload_2
       25:  invokeinterface #2,  1; //InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
       30:  astore  5
       32:  aload   5
       34:  invokeinterface #3,  1; //InterfaceMethod java/util/Iterator.hasNext:()Z
       39:  
    

    So like I said, the for-each loop is only syntactic sugar.

    it’s something I’m just fixated on.

    Move on, friendo.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Apache BCEL to dynamically create java classes that will each have its
is it possible to create a java application that will import excel files .
How can I create a barcode image in Java? I need something that will
I am unable to build my app because R.java will not update the ids.
In FleetTUI.java I have a list of Fleets (Each fleet will hold its own
I am fairly new to web programming, I have mainly used java to create
In java, When we call a new Constructor() , then a new object is
So far I understand Httpsession concepts in Java. HttpSession ses = req.getSession(true); will create
Will Java 8 support pattern matching like Scala and other functional programs do? I'm
Will Java code built and compiled against a 32-bit JDK into 32-bit byte code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.