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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:11:09+00:00 2026-06-18T16:11:09+00:00

Cloning the String arrays, using clone() method on java array. After cloning I’m expecting

  • 0

Cloning the String arrays, using clone() method on java array. After cloning I’m expecting to have new Strings in new array – with new addresses allocated for them. But… I got a bit different behavior, plz look at this:

(It will print:

same address
One

)

public class ArrayCopyClone {

    static String[] array2 = new String[] {"One", "Two", "Three"};

    public static void main(String[] args) {

        String[] copy2 = array2.clone();

        if (copy2[0] != array2[0])  {
            System.out.println("good");   // will never show up
        } else {
           System.out.println("same address");  // I'm expecting never be here
        }

        array2[0] = "new";

        System.out.println(copy2[0]); // "One", and this is OK (it means we have a copy)

    }

}

Is it related to string-shadowing? Should it be?

  • 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-18T16:11:10+00:00Added an answer on June 18, 2026 at 4:11 pm

    Cloning an array gives a shallow copy. So the contents are identical. For deep cloning see here.

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

Sidebar

Related Questions

I started cloning an SVN repository using the git-svn's clone operation. After about 6
After cloning an SVN repository using git-svn with the -s option ( git svn
Possible Duplicate: Java: recommended solution for deep cloning/copying an instance I have an object
I'm new to Java, Android, and SQLite, and I'm stuck at this. I have
I have a template string and an array of parameters that come from different
Possible Duplicate: Compare two Lists for differences I have following set of arrays string[]
I have the following method in some nemerle code: private static getLinks(text : string)
First of all, I'm new to Java. I'm trying to figure out what would
In Java, which is faster: Cloning an Object, then passing it to multiple listeners
I am trying to use 2 dimensional array in javascript for storing strings. But

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.