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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:23:55+00:00 2026-05-22T14:23:55+00:00

If I have a piece of code like this: MyClass[] objArray = new MyClass[7];

  • 0

If I have a piece of code like this:

MyClass[] objArray = new MyClass[7];
//assign values to objArray
//do something here
//sometime later
MyClass newObj = new MyClass();
objArray[3] = newObj;

The last statement above will do the following:

  • copy all the contents of the newObj to the space referred to by objArray[3].

Questions

  1. Am I right?

  2. Shallow copy or deep copy?

  3. If it is shallow copy, how can I make the deep copy possible?

    objArray[3] = newObj; 
    
  4. Does this rule applies to other Java container types, such as Queue, List, …?

  • 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-22T14:23:55+00:00Added an answer on May 22, 2026 at 2:23 pm

    Answer 1&2: No. Only a reference to the object is copied. newObj and objArray[3] will afterwards refer to the same object instance.

    Answer 3: If you want a copy, you have to implement it yourself. You could implement a copy constructor or Clonable, or for a simple deep copy, serialize and deserialize the object, but that requires it and all objects it consists of to be Serializable

    Answer 4: It’s exactly the same for all Java Objects: the reside on the heap, and the code works only with references to the objects. Container types usually implement a copy constructor that does a shallow copy. There is no deep copy functionality that is automatically available for all classes.

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

Sidebar

Related Questions

I have a piece of code like this: HashSet<Object> Set = new HashSet<Object>(); //add
I have a piece of code that looks like this: Dir.new(path).each do |entry| puts
If I have a piece of code which does something like this. try {
I often have the piece of code like this: it should do something, :focus
I have a piece of code like this: Class Program { static StreamReader sr
In the UI thread I have a piece of code that goes like this:
I have this piece of code which works, however I would like to change
I have a piece of code like so... FileInputStream fi = new FileInputStream(filein); GZIPInputStream
I have a piece of code like this - <script> var x,m,cloneNodex; function addVtier()
I have a piece of code like this class Base { public: Base(bool _active)

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.