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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:43:11+00:00 2026-05-18T08:43:11+00:00

How can I pass an array by reference in Java? For instance I need

  • 0

How can I pass an array by reference in Java? For instance I need to do some operations on the arrays A and B and populate the array C which should be available to the caller.

  public boolean Operate(int[] A, int[] B, int[] C)
  {
     //Write into the empty array C by manipulating values of A and B  and it should be accessible to caller
  }

I read that unlike C#, pass by reference is not there in Java. In that case what is the best way to do this.

  • 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-18T08:43:12+00:00Added an answer on May 18, 2026 at 8:43 am

    You are getting the term ‘pass by reference‘ confused with ‘pass (the reference of an object) by value‘ (which is what Java and C#, without out/ref do). In the above, if the contents of A, B or C are changed, they are changed because NO NEW OBJECT IS CREATED/CLONED/DUPLICATED when they are passed to the method. In C#, ‘ref’ and ‘out’ how one uses ‘pass by reference‘. Note that when ‘pass by reference‘ is used, assigning to a variable changes the value of the variable pass in by the caller (this is not possible in Java).

    See Wiki: Evaluation Strategies.

    Edit: If you really wish to follow this approach, remember that the calling convention does not dictate object mutability. Consider the following:

    void merge(int[] A, int[] B, int[][] C) {
        int[] t = internalMerge(A, B);
        C[0] = t;
    }
    int[][] icky = new int[1][0];
    merge(..., ..., icky);
    int[] r = icky[1];
    

    However, I would simply structure the code better. There is very little reason to use this approach and it introduces more side-effects and state mutations. As you can see, there is already a run-time error that the above code introduces. As others have suggested, why not simply return the more appropriate value?

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

Sidebar

Related Questions

We can pass reference of an array to a function like: void f(int (&a)[5]);
My current code looks like the following. How can I pass my array to
In C can I pass a multidimensional array to a function as a single
I can access anywhere inside the multi-dimensional an array via reference method. And I
I need to create a multi-dimensional array which will be passed to a class.
I need to solve the following question which i can't get to work by
i have a input tag which is non editable, but some times i need
I'm trying to pass an array of character arrays to a C function using
I have an array witch I pass to a function by reference to sort
In C++ if you pass a large array to a function, you need to

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.