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

  • Home
  • SEARCH
  • 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 7447031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:29:34+00:00 2026-05-29T12:29:34+00:00

(A question for those who know well the JVM compilation and optimization tricks… :-)

  • 0

(A question for those who know well the JVM compilation and optimization tricks… 🙂

Is there any of the “for” and “foreach” patterns clearly superior to the other?

Consider the following two examples:

public void forLoop(String[] text)
{
    if (text != null)
    {
        for (int i=0; i<text.length; i++)
        {
            // Do something with text[i]
        }
    }
}

public void foreachLoop(String[] text)
{
    if (text != null)
    {
        for (String s : text)
        {
            // Do something with s, exactly as with text[i]
        }
    }
}

Is forLoop faster or slower than foreachLoop?

Assuming that in both cases the text array did not need any do sanity checks, is there a clear winner or still too close to make a call?

EDIT: As noted in some of the answers, the performance should be identical for arrays, whereas the “foreach” pattern could be slightly better for Abstract Data Types like a List. See also this answer which discusses the subject.

  • 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-05-29T12:29:37+00:00Added an answer on May 29, 2026 at 12:29 pm

    From section 14.14.2 of the JLS:

    Otherwise, the Expression necessarily has an array type, T[]. Let L1 … Lm be the (possibly empty) sequence of labels immediately preceding the enhanced for statement. Then the meaning of the enhanced for statement is given by the following basic for statement:

    T[] a = Expression;
    L1: L2: ... Lm:
    for (int i = 0; i < a.length; i++) {
            VariableModifiersopt Type Identifier = a[i];
            Statement
    }
    

    In other words, I’d expect them to end up being compiled to the same code.

    There’s definitely a clear winner: the enhanced for loop is more readable. That should be your primary concern – you should only even consider micro-optimizing this sort of thing when you’ve proved that the most readable form doesn’t perform as well as you want.

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

Sidebar

Related Questions

my question is very simple for those who know the job very well. How
For those who know JQuery - must be effort less question - I'd like
Quick question: For those who don't know CakePHP Model->count(); always returns an integer. If
This is probably a very easy question for those who know regular expressions. What
This is a bit confused question for those who don't now advanced SQL... However.
As those who have worked on such a project you probably know this site:
Those who know C++ may know what I mean by 'unity build': *.cpp files
The title captures my question fairly well. I'm wondering if there is a good
Here's a question for those of you with experience in larger projects and API/framework
This question relates to those parts of the KenKen Latin Square puzzles which ask

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.