
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Deep Copy and Shallow Copy in Java
Both deep copy and shallow copy refer to creating a copy of the object given in different ways −
Shallow copy
This basically creates a new instance of the object and copies all the data from the original data set to the newly created instance. This means the newly created instance has to be specifically cast into the original object. It is also known as ‘shallow cloning’.
Example
import java.util.* ; class Demo{ private int[] my_data; public Demo(int[] my_vals){ my_data = my_vals; } public void display_data(){ System.out.println(Arrays.toString(my_data) ); } } public class Use_Demo{ public static void main(String[] args){ int[] my_vals = {56, 89, 91}; Demo my_inst = new Demo(my_vals); my_inst.display_data(); my_vals[0] = 65; my_inst.display_data(); } }
Output
[56, 89, 91] [65, 89, 91]
A class named Demo contains a variable, and a constructor that copies elements of the array into a new array. Another function named ‘display_data’ displays this array of data. In the main function, an instance is created, the array is defined, and the function is called. Relevant output is displayed on the console with all the changes reflecting in it.
Deep copy
This is used when a separate copy of the data is required for different purpose or usage. All the members of the class need to implement the ‘Cloneable’ interface and override the ‘clone’ method.
Example
import java.util.*; class Demo{ private int[] my_data; public Demo(int[] my_vals){ my_data = new int[my_vals.length]; for (int i = 0; i < my_data.length; i++){ my_data[i] = my_vals[i]; } } public void display_data(){ System.out.println(Arrays.toString(my_data)); } } public class Use_Demo{ public static void main(String[] args){ int[] my_vals = {56, 89, 91}; Demo my_inst = new Demo(my_vals); my_inst.display_data(); my_vals[0] = 65; my_inst.display_data(); } }
Output
[56, 89, 91] [56, 89, 91]
A class named Demo contains a variable, and a constructor that iterates through the array, and copies it to another array. Another function named ‘display_data’ displays this array of data. In the main function, an instance is created, the array is defined, and the function is called. Relevant output is displayed on the console with all the changes reflecting in it.
- Related Articles
- Copy - Shallow and deep copy operations in Python
- What is the difference between shallow copy and deep copy in Java?
- Python Shallow and Deep Copy operations
- What is Shallow Copy and how it is different from Deep Copy in C#?
- Return a shallow copy of IdentityHashMap in Java
- What is shallow copy? Explain with an example in Java.
- How to shallow copy the objects in JavaScript?
- How do you make a shallow copy of a list in Java?
- What is deep copy? Explain with an example in Java.
- How to create a shallow copy of Hashtable in C#?
- How to create a shallow copy of ArrayList in C#?
- Deep Copy of Struct Member Arrays in C
- How to create a shallow copy of the BitArray in C#?
- How to create a shallow copy of SortedList Object in C#?
- How would you deep copy an Object in Javascript?
