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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:55:05+00:00 2026-05-27T09:55:05+00:00

I have a loop like: String tmp; for(int x = 0; x < 1000000;

  • 0

I have a loop like:

String tmp;

for(int x = 0; x < 1000000; x++) {


   // use temp


   temp = ""; // reset
}

This string is holding at most 10 characters.

What would be the most effecient way of creating a variable for this use case?

Should I use a fixed size array? Or a stringbuffer instead?

I don’t want to create 1million variables when I don’t have to, and it matters for this method (performance).

Edit
I simplified my scenerio, I actually need this variable to be at the class level scope as there are some events that take place i.e. it can’t be declared within the loop.

  • 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-27T09:55:05+00:00Added an answer on May 27, 2026 at 9:55 am

    Why not simply declare temp inside the loop like so:

    for(int x = 0; x < 1000000; x++) {
        String temp;
        // use temp
    }
    

    You even get a very (very, very) slight performance increase because you don’t have to waste time resetting the value of temp to "".

    With regards to your update, It still depends on what you do with temp but a StringBuffer would probably be the easiest to use. And especially if you need to concatenate together a Sting, it would be quite fast.

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

Sidebar

Related Questions

I have two nested loop in XSL like this, at this moment I use
I'd like avoid loop I have this : string s = AAAA,12,BBBB,34,CCCCC,56; With Linq,
I have this loop: for (Map.Entry<Integer, String> entry: collection.entrySet()) { } I'd like to
I have a for loop like this : String myString = 123456789; String prefix
I have a loop like this: for i=1:no %some calculations fid = fopen('c:\\out.txt','wt'); %write
Say I have a simple PHP loop like this one // Bad example $array
What would be the correct way to find a string like this in a
I have a string like the following in C#. I need to loop through
In scripting languages like PHP having a for loop like this would be a
We have an application that runs a routine like: protected string _largeFile; Execute() //this

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.