answersLogoWhite

0

What is shallow copy in Java?

Updated: 11/1/2022
User Avatar

Wiki User

14y ago

Best Answer

A shallow copy in Java refers to the copying of object references from one collection to another.

In contrast, a deep copy in Java refers to the copying of actual object data from one collection to another.

// shallow copy

Object[] original = {new Object(), new Object(), new Object()};

Object[] copy = new Object[original.length];

for(int i = 0; i < original.length; ++i)

copy[i] = original[i];

// deep copy

Object[] original = {new Object(), new Object(), new Object()};

Object[] copy = new Object[original.length];

for(int i = 0; i < original.length; ++i)

// this would not compile in this case because Object does not have a publicly

// accessible clone() method. if you were to use this, make sure that the type

// has an accessible clone() method

copy[i] = original[i].clone();

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is shallow copy in Java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Does Java support copy constructor?

No. Java does not support copy constructor


What is copy constructor in java?

Java, unlike C++ does not support copy constructors.


What is the difference in shallow copy and deep copy in mobile application development process?

Shallow copy also known as address copy involves copying of the address and not the actual data. In case of deep copy, the data is copied.


What actors and actresses appeared in Shallow Copy - 2009?

The cast of Shallow Copy - 2009 includes: Jolee Kim as Tabitha Demetrius Sager as Will Michael Thomas Moore as Max


Where can one purchase a copy of the book Java for dummies?

A copy of the book Java For Dummies can be purchased from all good bookstores, such as Waterstones. Alternatively, try webpage stores such as Amazon or Ebay.


What is difference between Deep copy and shallow copy in iphone?

Shallow copy is duplicate little as possible. because of it copy one object to another object. it copy only structure not elements . Deep copy means Duplicate everything. it will be copy structure as well as elements; e.g :- char A*={'a','b','c'}; char B*={'x','y','z'}; B=A;


Where can you find roms for java games?

A ROM image for a video game is basically a software copy of a hardware chip. There is no such thing for Java, or any game written in Java.


Where can one download Java SE 6?

There are many places one might go to download a copy of Java SE 6 software. In addition to the official Java website, one might also try the CNET website.


Does it matter if your computer has java or not?

No really, Java is not used for your computer to perform a critical task in your system. A normal user only use java for execute programs written in java that they copy in their systems or they download from internet. If you do not have java in your computer the mayor problems you could experiment will be that you can not play some online games from internet or install the netbeans.


Program in Java to create file and copy content of an existing file to it?

You need to use File class to create file in java and than Reader class implementation such as BufferedReder to read content.


How do you download java in your Nintendo DSi?

Get a sd or sdhc cardget a sd card readerplug it in a computerthen copy the java or if you also want adobe and drag it on the sd cardput the sd card in a dsi and go to "system settings"Go to "sd card managment"and copy the files and it's Done!!!!!!!!!!!!!!!


What is difference between Deep copy and shallow copy?

A "shallow" copy is when the member values are physically copied from one object to another, *including* the values of any pointer or reference members. If there are pointer or reference memebrs, then, those poointers or references refer to the *same* objects as the original object, which is usually a bad thing. That's why you want to define a copy constructor and assignment operator for objects that contain pointers or references. It's called a "shallow" copy because only the values of the pointers/references are copied, instead of making copies of those referred-to objects and setting pointers to them. *That* is what would be called a "deep" copy, because it's going "deeper" into the structure, copying everything, not just the first "layer".