Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // deep copying in Java
- // 2026-04-16
- // tested on Win with javac 21.0.2
- import java.util.*;
- class DeepCopy implements Cloneable { // uncomment this for copying to work
- // this is the field that requires care
- // note the different between C++
- // in C++ you specify the pointer with '*'
- // but in Java you have to know what is a pointer/reference
- // and what isn't - arrays are as well as all the objects
- int [] data = new int [10];
- // this is the method for object copying which every class in Java has
- // it has some issues so if you want you can check the 'copy' method
- // you should not write copy constructors in Java because of how
- // Objects are saved, i.e. they are always references/pointers, and
- // this means you do not know which object it points to
- // (it becomes important when we learn inheritence, more on this later)
- // We need a method that would allow the object to create a copy
- // of itself without the outside world needing to know what
- // its type is. If we use clone - we must implement Cloneable interface
- // because otherwise Object.clone() would throw the exception,
- // so we do, and the contract says that for the object to properly
- // copy each layer we must call super.clone() in the beginning and then
- // add instructions to copy this layer, as Object construction in Java
- // is done in stages, i.e. first Object is created and then extended
- // into your own custom class with its field and methods.
- // You can see clone() throw CloneNotSupportedException in many
- // examples on the internet. We avoid this by catching it inside,
- // so our main does not throw it.
- public DeepCopy clone(){
- try {
- // we first call super.clone and cast from Object
- DeepCopy copy = (DeepCopy)super.clone();
- // and now properly copy the object
- copy.data = new int[10];
- for(int i = 0; i < 10; ++i){
- // note the difference:
- // in C++ we modify ourselves based on the copy
- // and in Java we fill the copy based on ourselves
- copy.data[i] = data[i];
- }
- return copy;
- } catch(CloneNotSupportedException ce){
- // should be unreachable because we implemented Cloneable
- System.out.println("Copying is not supported!");
- return null;
- }
- }
- // .. or just write your own copy from scratch
- // and thus you do not need all the "baggage"
- public DeepCopy copy(){
- DeepCopy copy = new DeepCopy();
- copy.data = new int[10];
- for(int i = 0; i < 10; ++i){
- copy.data[i] = data[i];
- }
- return copy;
- }
- public static void main(String [] args){
- // create to objects with arrays within them
- // and print the data to screen
- DeepCopy dc1 = new DeepCopy();
- DeepCopy dc2 = new DeepCopy();
- for(int i = 0; i < 10; ++i){
- System.out.print(" " + dc1.data[i]);
- }
- System.out.println();
- for(int i = 0; i < 10; ++i){
- System.out.print(" " + dc2.data[i]);
- }
- System.out.println();
- // this is the copying part
- dc1 = dc2;
- // now we change one of the objects
- // and if the copies are independent is should not
- // affect the other copy
- dc1.data[5] = 5;
- // but it does as we see here
- for(int i = 0; i < 10; ++i){
- System.out.print(" " + dc1.data[i]);
- }
- System.out.println();
- for(int i = 0; i < 10; ++i){
- System.out.print(" " + dc2.data[i]);
- }
- System.out.println();
- // so copy it properly
- dc1 = dc2.clone();
- dc1.data[2] = 5;
- // now check
- for(int i = 0; i < 10; ++i){
- System.out.print(" " + dc1.data[i]);
- }
- System.out.println();
- for(int i = 0; i < 10; ++i){
- System.out.print(" " + dc2.data[i]);
- }
- System.out.println();
- // or don't use clone
- dc1 = dc2.copy();
- dc1.data[7] = 5;
- // now check
- for(int i = 0; i < 10; ++i){
- System.out.print(" " + dc1.data[i]);
- }
- System.out.println();
- for(int i = 0; i < 10; ++i){
- System.out.print(" " + dc2.data[i]);
- }
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment