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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:56:23+00:00 2026-05-23T02:56:23+00:00

I have these two snippets of code (written in Java) which is more more

  • 0

I have these two snippets of code (written in Java) which is more more efficient?

int foo(){

   int result;
   for(int i = 0; i < n;i++){
      SomeObject a,b,c;
      a = new SomeObject();
      b = new SomeObject();
      c = new SomeObject();
      //do something with a,b,c
      //and derive result
   }
   return result;
}

or

int foo(){

   int result;
   SomeObject a,b,c;
   a = new SomeObject();
   b = new SomeObject();
   c = new SomeObject();

   for(int i = 0; i < n;i++){
      a.flush(); //reset object do not create new though
      b.flush(); //reset object do not create new though
      c.flush(); //reset object do not create new though
      //do something with a,b,c
      //and derive result
   }
   return result;
}

In the second snippet, I have moved the local variables out of the loop, so effectively it creates only one instance of them. Does this improve anything?
Logically, it would make sense when the variables are inside the loop. But would the garbage collector clean up the objects efficiently?

EDIT: Updated the snipped, regarding instantiation of objects.

  • 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-23T02:56:23+00:00Added an answer on May 23, 2026 at 2:56 am

    You’ve moved the variables out, but those aren’t objects. Assuming you’re assigning different values to a, b and c on every iteration of the loop, you haven’t reduced memory allocation at all – you’ve just widened the scope of variables unnecessarily.

    If you don’t need to change the values of a, b and c in the loop then that’s a different matter, and it would be a better idea to assign them outside the loop rather than on every iteration.

    The main thing is to realize the difference between objects and variables though. In particular, creating “new” variables on each iteration of the loop doesn’t actually cost anything.

    EDIT: Okay, with the updated question, there clearly is a difference in terms of how many objects are created. However, depending on exactly what’s going on the second form may still be more preferable: it’s certainly easier to understand, and it doesn’t rely on the details of what the flush operation does. On the other hand, if creating an instance of SomeObject is expensive (e.g. it generates crypto keys) then it’s a good idea to optimize against that.

    In short: it depends. Write the most readable code first, measure it, and compare that with your performance requirements. Only move to less obvious code when there’s a demonstrable benefit.

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

Sidebar

Related Questions

I have these two pieces of code, wich one is more readable? foreach decimal
I have two JavaScript code snippets. These perform specific task when an 'Update' button
I have seen these two approaches for constant declaration which are to be used
I have seen code around with these two styles , I am not not
I have these two pieces of code. The first one works perfectly: UIView *tmp
I have some problem with these code snippets: CSS: #wrapper{ width: 600px; background: gray;
I have two snippets of code that tries to convert a float list to
So i have these two jquery functions which pass my XHR service a key
I have these two lines in my code being developed in HTML5: context.rotate(radian); radian
Say you have these two methods: Number 1: void AddPerson(Person person) { // Validate

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.