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 40839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:05:11+00:00 2026-05-10T15:05:11+00:00

String s = ; for(i=0;i<….){ s = some Assignment; } or for(i=0;i<..){ String s

  • 0
String s = ''; for(i=0;i<....){     s = some Assignment; } 

or

for(i=0;i<..){     String s = some Assignment; } 

I don’t need to use ‘s’ outside the loop ever again. The first option is perhaps better since a new String is not initialized each time. The second however would result in the scope of the variable being limited to the loop itself.

EDIT: In response to Milhous’s answer. It’d be pointless to assign the String to a constant within a loop wouldn’t it? No, here ‘some Assignment’ means a changing value got from the list being iterated through.

Also, the question isn’t because I’m worried about memory management. Just want to know which is better.

  • 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. 2026-05-10T15:05:12+00:00Added an answer on May 10, 2026 at 3:05 pm

    Limited Scope is Best

    Use your second option:

    for ( ... ) {   String s = ...; } 

    Scope Doesn’t Affect Performance

    If you disassemble code the compiled from each (with the JDK’s javap tool), you will see that the loop compiles to the exact same JVM instructions in both cases. Note also that Brian R. Bondy’s ‘Option #3’ is identical to Option #1. Nothing extra is added or removed from the stack when using the tighter scope, and same data are used on the stack in both cases.

    Avoid Premature Initialization

    The only difference between the two cases is that, in the first example, the variable s is unnecessarily initialized. This is a separate issue from the location of the variable declaration. This adds two wasted instructions (to load a string constant and store it in a stack frame slot). A good static analysis tool will warn you that you are never reading the value you assign to s, and a good JIT compiler will probably elide it at runtime.

    You could fix this simply by using an empty declaration (i.e., String s;), but this is considered bad practice and has another side-effect discussed below.

    Often a bogus value like null is assigned to a variable simply to hush a compiler error that a variable is read without being initialized. This error can be taken as a hint that the variable scope is too large, and that it is being declared before it is needed to receive a valid value. Empty declarations force you to consider every code path; don’t ignore this valuable warning by assigning a bogus value.

    Conserve Stack Slots

    As mentioned, while the JVM instructions are the same in both cases, there is a subtle side-effect that makes it best, at a JVM level, to use the most limited scope possible. This is visible in the ‘local variable table’ for the method. Consider what happens if you have multiple loops, with the variables declared in unnecessarily large scope:

    void x(String[] strings, Integer[] integers) {   String s;   for (int i = 0; i < strings.length; ++i) {     s = strings[0];     ...   }   Integer n;   for (int i = 0; i < integers.length; ++i) {     n = integers[i];     ...   } } 

    The variables s and n could be declared inside their respective loops, but since they are not, the compiler uses two ‘slots’ in the stack frame. If they were declared inside the loop, the compiler can reuse the same slot, making the stack frame smaller.

    What Really Matters

    However, most of these issues are immaterial. A good JIT compiler will see that it is not possible to read the initial value you are wastefully assigning, and optimize the assignment away. Saving a slot here or there isn’t going to make or break your application.

    The important thing is to make your code readable and easy to maintain, and in that respect, using a limited scope is clearly better. The smaller scope a variable has, the easier it is to comprehend how it is used and what impact any changes to the code will have.

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

Sidebar

Ask A Question

Stats

  • Questions 165k
  • Answers 165k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer To go back you simply call: - (IBAction)switchToBaseView:(id)sender { [self… May 12, 2026 at 12:31 pm
  • Editorial Team
    Editorial Team added an answer The problem here seems to have been caused by the… May 12, 2026 at 12:31 pm
  • Editorial Team
    Editorial Team added an answer For example in Scheme you have the flexibility to include… May 12, 2026 at 12:31 pm

Related Questions

String s = ; for(i=0;i<....){ s = some Assignment; } or for(i=0;i<..){ String s
There is a bug in Firefox (even in the new betas and in minefield
Is there a way in Java's for-each loop for(String s : stringArray) { doSomethingWith(s);
I requested 100 pages that all 404. I wrote { var s = DateTime.Now;
I've discovered that std::string s are very slow compared to old-fashioned null-terminated strings, so

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.