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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:49:57+00:00 2026-05-24T19:49:57+00:00

Suppose I have an int-array and I want to modify it. I know that

  • 0

Suppose I have an int-array and I want to modify it. I know that I cannot assign a new array to array passed as parameter:

public static void main(String[] args)
{
    int[] temp_array = {1};
    method(temp_array);
    System.out.println(temp_array[0]); // prints 1
}
public static void method(int[] n)
{
    n = new int[]{2};
}

while I can modify it:

public static void main(String[] args)
{
    int[] temp_array = {1};
    method(temp_array);
    System.out.println(temp_array[0]); // prints 2
}
public static void method(int[] n)
{
    n[0] = 2;
}

Then, I tried to assign an arbitrary array to the array passed as parameter using clone():

public static void main(String[] args)
{
    int[] temp_array = {1};
    method(temp_array);
    System.out.println(temp_array[0]); // prints 1 ?!
}
public static void method(int[] n)
{
    int[] temp = new int[]{2};
    n = temp.clone();
}

Now, I wonder why it prints 1 in last example while I’m just copying the array with clone() which it’s just copying the value not the reference. Could you please explain that for me?


EDIT: Is there a way to copy an array to object without changing the reference? I mean to make last example printing 2.

  • 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-24T19:49:58+00:00Added an answer on May 24, 2026 at 7:49 pm

    Your examples 1 and 3 are virtually the same in context of the question – you are trying to assign a new value to n (which is a reference to an array passed by value).

    The fact that you cloned temp array doesn’t matter – all it did was create a copy of temp and then assign it to n.

    In order to copy values into array passed into your method method you might want to look at:System.arraycopy

    It all, of course, depends on the sizes of your n array and the one you create inside method method.

    Assuming they both have the same length, for example, you would do it like that:

    public static void main(String[] args)
    {
        int[] temp_array = {1};
        method(temp_array);
        System.out.println(temp_array[0]);
    }
    public static void method(int[] n)
    {
        int[] temp = new int[]{2};
        System.arraycopy(temp, 0, n, 0, n.length); 
        // or System.arraycopy(temp, 0, n, 0, temp.length) - 
        // since we assumed that n and temp are of the same length
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

suppose that we have three array int a[]=new int[]{4,6,8,9,11,12}; int b[]=new int[]{3,5,7,13,14}; int c[]=new
Suppose I have a method public Patient(int id) { ---- } that returns Patient
Suppose I have a sorted array of integers int[] , and I want to
Suppose I have one long long int and want to take its bits and
Suppose I have one list: IList<int> originalList = new List<int>(); originalList.add(1); originalList.add(5); originalList.add(10); And
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Suppose I have a list or array of items, and I want to sum
Supposed i have an array int[] arr = {1,2,3,4} I want to convert it
Suppose I have the following arrays: List<int[]> numbers = new List<int[]>(); numbers.Add(new int[] {
Suppose I have an array of class values in Java and I want 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.