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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:17:24+00:00 2026-05-20T07:17:24+00:00

I have an array of type string which I want to re-use inside a

  • 0

I have an array of type string which I want to re-use inside a method.

I need to pass it to a function which returns a subset of the elements of array passed. To capture the returned array from function, should I declare a new string array or can I safely re-use same array that was holding the unfiltered array to hold filtered array ??

Also clarify that, when I pass a variable like array to a function does it creates a new space for the array in the heap each time I pass it as parameter to further functions or just it uses the same space in the heap & just passes the reference? I guess in case of array it passes just the reference but in case of simple variables it allocates new space on stack, right?

EDIT: Thanks you all for the great answers and explanations!!

Please also clarify whether that if I am reusing the array, would I be required to set the null at the position when I end the subset of array ? (Actually the subset of my array is not directly calculated from the array but some other calulations, so I would be required to manually set the null otherwise the older elements of the list would be visible, right??)

  • 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-20T07:17:24+00:00Added an answer on May 20, 2026 at 7:17 am

    when I pass a variable like array to a function does it creates a new space for the array in the heap each time I pass it as parameter to further functions or just it uses the same space in the heap & just passes the reference

    Java is entirely pass by value, but with arrays and object types, the value being passed is a reference (pointer) to the actual object, which is not duplicated. So when you pass an array into a function, only the reference to the array is copied; the array itself isn’t. That’s why if a function modifies the contents of an array, the code calling the function sees the modifications.

    Example:

    // A method that changes the first entry in an array
    void changeArray(String[] theArray) {
        theArray[0] = "Updated";
    }
    
    // A method that uses the above
    void someMethod() {
        String[] foo = new String[3];
        foo[0] = "Original 0";
        foo[1] = "Original 1";
        foo[2] = "Original 2";
    
        this.changeArray(foo); // `changeArray` receives a *copy* of the reference
                               // held in `foo`; both references point to the same
                               // array
    
        System.out.println(foo[0]); // Prints "Updated"
    }
    

    Passing a reference into a method is exactly like assigning it to another reference variable:

    String[] foo = new String[3]; // Create a new aaray, assign reference to `foo` variable
    String[] bar = foo;           // Copy the reference into `bar` as well
    foo[0] = "Hi there";          // Use the reference in `foo` to assign to the first element
    System.out.println(bar[0]);   // Use the reference in `bar`, prints "Hi there"
    

    Essentially, references are values (numbers) that tell the JVM where the data for an object is. So even when we copy a reference (because Java is entirely pass-by-value, the value of the reference is passed to the function), it still points to the same memory as the original.

    Since the reference is passed into the function by value, the function can’t change the calling code’s reference:

    // A method that assigns a new reference; the calling code sees no change
    void changeReference(String[] theArray) {
        theArray = new String[1];        // Now we're not using the caller's object anymore
        theArray[0] = "I was changed";
    }
    
    // A method that uses the above
    void someMethod() {
        String[] foo = new String[3];
        foo[0] = "Original 0";
        foo[1] = "Original 1";
        foo[2] = "Original 2";
    
        this.changeReference(foo);
        System.out.println(foo[0]); // Prints "Original 0", our reference wasn't changed
    }
    

    You’ll note that this is exactly how primitives are treated:

    void whatever(int a) {
        a = 5;
    }
    void someMethod() {
        int foo;
    
        foo = 3;
        whatever(foo);
        System.out.println(foo); // Prints 3, of course, not 5
    }
    

    …and in fact, object references (as opposed to objects) are primitives.

    …should I declare a new string array or can I safely re-use same array that was holding the unfiltered array to hold filtered array

    You can safely re-use it if that’s appropriate within the scope of your function. It may be more appropriate to declare a new variable (e.g., filteredThingy), but that’s a matter of style and will depend on the situation.

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

Sidebar

Related Questions

I have an array of different type objects and I use a BinaryWriter to
I'm looking for a kind of array data-type that can easily have items added,
I have a vector-like class that contains an array of objects of type T
I have an array of hashes, and I want the unique values out of
I have a 2 dimensional byte array I[][], each entry in which is either
I have an array in Perl: my @my_array = (one,two,three,two,three); How do I remove
I have an array of numbers that potentially have up to 8 decimal places
I have an array of a few million numbers. double* const data = new
I have an array of items that are time sensitive. After an amount of
I have an array of 1000 strings to load into a combo box. What

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.