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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:54:44+00:00 2026-06-16T21:54:44+00:00

What exactly does the clone() method in Java return when used on an array?

  • 0

What exactly does the clone() method in Java return when used on an array?
Does it return a new array with data copied from the original?

Ex:

int[] a = {1,2,3};
int[] b = a.clone();
  • 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-06-16T21:54:45+00:00Added an answer on June 16, 2026 at 9:54 pm

    When the clone method is invoked upon an array, it returns a reference to a new array which contains (or references) the same elements as the source array.

    So in your example, int[] a is a separate object instance created on the heap and int[] b is a separate object instance created on the heap. (Remember all arrays are objects).

        int[] a = {1,2,3};
        int[] b = a.clone();
    
        System.out.println(a == b ? "Same Instance":"Different Instance");
        //Outputs different instance
    

    If were to modify int[] b the changes would not be reflected on int[] a since the two are separate object instances.

        b[0] = 5;
        System.out.println(a[0]);
        System.out.println(b[0]);
        //Outputs: 1
        //         5
    

    This becomes slightly more complicated when the source array contains objects. The clone method will return a reference to a new array, which references the same objects as the source array.

    So if we have the class Dog…

        class Dog{
    
            private String name;
    
            public Dog(String name) {
                super();
                this.name = name;
            }
    
            public String getName() {
                return name;
            }
    
            public void setName(String name) {
                this.name = name;
            }
    
        }
    

    and I create and populate an array of type Dog…

        Dog[] myDogs = new Dog[4];
    
        myDogs[0] = new Dog("Wolf");
        myDogs[1] = new Dog("Pepper");
        myDogs[2] = new Dog("Bullet");
        myDogs[3] = new Dog("Sadie");
    

    then clone dog…

        Dog[] myDogsClone = myDogs.clone();
    

    the arrays refer to the same elements…

        System.out.println(myDogs[0] == myDogsClone[0] ? "Same":"Different");
        System.out.println(myDogs[1] == myDogsClone[1] ? "Same":"Different");
        System.out.println(myDogs[2] == myDogsClone[2] ? "Same":"Different");
        System.out.println(myDogs[3] == myDogsClone[3] ? "Same":"Different");
        //Outputs Same (4 Times)
    

    This means if we modify an object accessed through the cloned array, the changes will be reflected when we access the same object in the source array, since they point to the same reference.

        myDogsClone[0].setName("Ruff"); 
        System.out.println(myDogs[0].getName());
        //Outputs Ruff
    

    However, changes to the array itself will only affect that array.

        myDogsClone[1] = new Dog("Spot");
        System.out.println(myDogsClone[1].getName());
        System.out.println(myDogs[1].getName());
        //Outputs Spot
        //        Pepper
    

    If you generally understand how object references work, it is easy to understand how arrays of objects are impacted by cloning and modifications. To gain further insight into references and primitives I would suggest reading this excellent article.

    Gist of Source Code

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

Sidebar

Related Questions

What exactly does SignedCookieJar do? And what is the difference from that and using
I'm just wondering how exactly does a delegate method know when to be called?
Suppose I have a function void myFun(int*) In C++ what exactly does the following
How exactly does adding a random number to the end of an AJAX server
What exactly does putting extern C into C++ code do? For example: extern C
What exactly does a mobile need to be able to run JavaFX? Can it
What exactly does a finally block in exception handling perform?
What exactly does the Objective-C garbage collector collect? For example, if I'm writing a
What exactly does execve() do? I've tried looking at the documentation (http://linux.die.net/man/2/execve) but given
I was wondering how exactly does TCP implement in-order delivery. lets say this is

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.