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

The Archive Base Latest Questions

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

String abc[]={abc}; String def[]={}; def=abc; def[0]=def[0]+changed; System.out.println(abc[0]); by changing def object, my abc object

  • 0
String abc[]={"abc"};
String def[]={};

def=abc;
def[0]=def[0]+"changed";
System.out.println(abc[0]);

by changing “def” object, my abc object is changed as well. Beside String[] array has this characteristic what other java object has similar characteristic? can explain more? in order to prevent abc from changed when i changed def, i will have to do def = abc.clone();

  • 1 1 Answer
  • 1 View
  • 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-14T04:18:43+00:00Added an answer on May 14, 2026 at 4:18 am

    You are confusing object mutability/immutability with copying of reference values.

    In these diagrams, [var/index] is a reference variable, and {{an Object}} is an object.

    String abc[]={"abc"};
    String def[]={};
    
       [abc] ------> {{a String[1]}}
                     [0] --------------> {{a String "abc"}}
    
       [def] ------> {{a String[0]}}
    

    Now you make def reference variable points to the same object as abc reference variable:

    def=abc;
    
       [abc] ------> {{a String[1]}}
                  /  [0] --------------> {{a String "abc"}}
                 /
       [def] ---/    {{a String[0]}}
    

    At this point, the array of length zero is unreferenced, and should be garbage-collectable. We can narrow our discussion to the array of length one. Note that a String[] is an array of references. With this next line, you changed what the only element in the length one array points to.

    def[0]=def[0]+"changed";
    
       [abc] ------> {{a String[1]}}
                  /  [0] ---------\      {{a String "abc"}}
                 /                 \
       [def] ---/                   \--> {{a String "abcchanged"}}
    

    Note that {{a String "abc"}} itself was not mutated. [abc] and [def] now points to the same {{a String[1]}}, which is mutable (i.e. you can make the elements of the array, which are references to String objects, to point to anything).


    in order to prevent abc from changed when i changed def, i will have to do def = abc.clone();

    Actually, that’s not quite accurate. Let’s see what happens if you clone() an array of references to a mutable type StringBuilder.

        StringBuilder[] abc = new StringBuilder[] { new StringBuilder("Hello") };
        StringBuilder[] def = abc.clone();
        def[0].append(" world!");
        System.out.println(abc[0]); // prints "Hello world!"
    

    I won’t make the diagrams for you this time, but you can easily draw it out on paper. What’s happening here is that even though clone() makes a second {{a StringBuilder[1]}} object with its own element (i.e. def != abc), that element is pointing to the same {{a StringBuilder}} object (i.e. def[0] == abc[0]).


    In short:

    • Immutability means that objects of a certain type can not change in any meaningful way to outside observers
      • Integer, String, etc are immutable
      • Generally all value types should be
    • Array objects are mutable
      • It may be an array of references to immutable types, but the array itself is mutable
        • Meaning you can set those references to anything you want
        • Also true for array of primitives
      • An immutable array will not be practical
    • References to objects can be shared
      • If the object is mutable, mutation will be seen through all these references

    If you want more in-depth understanding of the issues, I recommend the following:

    • Is Java pass by reference? — NO!
    • Are Java function parameters always passed-by-value? — YES!
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 385k
  • Answers 385k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Workaround I created an aspx page to call the GSA… May 14, 2026 at 11:32 pm
  • Editorial Team
    Editorial Team added an answer Jetty, Tomcat and Glassfish are all Servlet containers (well, Glassfish… May 14, 2026 at 11:32 pm
  • Editorial Team
    Editorial Team added an answer libdl is very well suited to plug-in architectures - within… May 14, 2026 at 11:32 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.