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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:40:19+00:00 2026-05-25T06:40:19+00:00

I come from a C background, so I admit that I’m still struggling with

  • 0

I come from a C background, so I admit that I’m still struggling with letting go of memory management when writing in Java. Here’s one issue that’s come up a few times that I would love to get some elaboration on. Here are two ways to write the same routine, the only difference being when double[] array is declared:

Code Sample 1:

double[] array;
for (int i=0; i<n; ++i) {
    array = calculateSomethingAndReturnAnArray(i);
    if (someFunctionOnArrays(array)) {
        // DO ONE THING
    } else {
        // DO SOME OTHER THING
    }
}

Code Sample 2:

for (int i=0; i<n; ++i) {
    double[] array = calculateSomethingAndReturnAnArray(i);
    if (someFunctionOnArrays(array)) {
        // DO ONE THING
    } else {
        // DO SOME OTHER THING
    }
}

Here, private double[] calculateSomethingAndReturnAnArray(int i) always returns an array of the same length. I have a strong aversion to Code Sample 2 because it creates a new array for each iteration when it could just overwrite the existing array. However, I think this might be one of those times when I should just sit back and let Java handle the situation for me.

What are the reasons to prefer one of the ways over the other or are they truly identical in Java?

  • 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-25T06:40:20+00:00Added an answer on May 25, 2026 at 6:40 am

    There’s nothing special about arrays here because you’re not allocating for the array, you’re just creating a new variable, it’s equivalent to:

    Object foo;
    for(...){
        foo = func(...);
    }
    

    In the case where you create the variable outside the loop it, the variable (which will hold the location of the thing it refers to) will only ever be allocated once, in the case where you create the variable inside the loop, the variable may be reallocated for in each iteration, but my guess is the compiler or the JIT will fix that in an optimization step.

    I’d consider this a micro-optimization, if you’re running into problems with this segment of your code, you should be making decisions based on measurements rather than on the specs alone, if you’re not running into issues with this segment of code, you should do the semantically correct thing and declare the variable in the scope that makes sense.

    See also this similar question about best practices.

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

Sidebar

Related Questions

I come from Java background, where we have data structures with interfaces that if
I come from Java Background and so used to Debugging using Eclipse but have
I come from a Java background and have been using C# for the last
I come from a .net background so the empty classes (models) that I'm seeing
I come from a background where I normally create one file per class. I
I come from a Java background where the web application is always resident in
I come from a Java background. I am wanting to learn more about concurrency
So I come from a background in Java where you can create a class
Sorry if the question is silly. I come from java background. In the following
I come from a C# background and have recently started learning C++. One of

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.