irmantas_radavicius

Untitled

Apr 16th, 2026
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.95 KB | None | 0 0
  1. // deep copying in Java
  2. // 2026-04-16
  3. // tested on Win with javac 21.0.2
  4.  
  5. import java.util.*;
  6.  
  7. class DeepCopy implements Cloneable { // uncomment this for copying to work
  8.    
  9.     // this is the field that requires care
  10.     // note the different between C++
  11.     // in C++ you specify the pointer with '*'
  12.     // but in Java you have to know what is a pointer/reference
  13.     // and what isn't - arrays are as well as all the objects
  14.     int [] data = new int [10];
  15.  
  16.     // this is the method for object copying which every class in Java has
  17.     // it has some issues so if you want you can check the 'copy' method
  18.     // you should not write copy constructors in Java because of how
  19.     // Objects are saved, i.e. they are always references/pointers, and
  20.     // this means you do not know which object it points to
  21.     // (it becomes important when we learn inheritence, more on this later)
  22.     // We need a method that would allow the object to create a copy
  23.     // of itself without the outside world needing to know what
  24.     // its type is. If we use clone - we must implement Cloneable interface
  25.     // because otherwise Object.clone() would throw the exception,
  26.     // so we do, and the contract says that for the object to properly
  27.     // copy each layer we must call super.clone() in the beginning and then
  28.     // add instructions to copy this layer, as Object construction in Java
  29.     // is done in stages, i.e. first Object is created and then extended
  30.     // into your own custom class with its field and methods.
  31.     // You can see clone() throw CloneNotSupportedException in many
  32.     // examples on the internet. We avoid this by catching it inside,
  33.     // so our main does not throw it.
  34.     public DeepCopy clone(){
  35.         try {  
  36.             // we first call super.clone and cast from Object
  37.             DeepCopy copy = (DeepCopy)super.clone();
  38.            
  39.             // and now properly copy the object
  40.             copy.data = new int[10];
  41.             for(int i = 0; i < 10; ++i){
  42.                 // note the difference:
  43.                 // in C++ we modify ourselves based on the copy
  44.                 // and in Java we fill the copy based on ourselves
  45.                 copy.data[i] = data[i];
  46.             }
  47.             return copy;
  48.            
  49.         } catch(CloneNotSupportedException ce){
  50.             // should be unreachable because we implemented Cloneable
  51.             System.out.println("Copying is not supported!");
  52.             return null;
  53.         }      
  54.     }
  55.    
  56.     // .. or just write your own copy from scratch
  57.     // and thus you do not need all the "baggage"
  58.     public DeepCopy copy(){    
  59.         DeepCopy copy = new DeepCopy();    
  60.         copy.data = new int[10];
  61.         for(int i = 0; i < 10; ++i){
  62.             copy.data[i] = data[i];
  63.         }
  64.         return copy;
  65.        
  66.     }
  67.  
  68.  
  69.     public static void main(String [] args){
  70.         // create to objects with arrays within them
  71.         // and print the data to screen
  72.         DeepCopy dc1 = new DeepCopy();
  73.         DeepCopy dc2 = new DeepCopy();
  74.         for(int i = 0; i < 10; ++i){
  75.             System.out.print(" " + dc1.data[i]);
  76.         }
  77.         System.out.println();
  78.         for(int i = 0; i < 10; ++i){
  79.             System.out.print(" " + dc2.data[i]);
  80.         }
  81.         System.out.println();
  82.        
  83.         // this is the copying part
  84.         dc1 = dc2;
  85.        
  86.         // now we change one of the objects
  87.         // and if the copies are independent is should not
  88.         // affect the other copy
  89.         dc1.data[5] = 5;
  90.        
  91.         // but it does as we see here
  92.         for(int i = 0; i < 10; ++i){
  93.             System.out.print(" " + dc1.data[i]);
  94.         }
  95.         System.out.println();
  96.         for(int i = 0; i < 10; ++i){
  97.             System.out.print(" " + dc2.data[i]);
  98.         }
  99.         System.out.println();
  100.        
  101.         // so copy it properly
  102.         dc1 = dc2.clone();
  103.         dc1.data[2] = 5;
  104.        
  105.         // now check
  106.         for(int i = 0; i < 10; ++i){
  107.             System.out.print(" " + dc1.data[i]);
  108.         }
  109.         System.out.println();
  110.         for(int i = 0; i < 10; ++i){
  111.             System.out.print(" " + dc2.data[i]);
  112.         }
  113.         System.out.println();
  114.        
  115.         // or don't use clone
  116.         dc1 = dc2.copy();
  117.         dc1.data[7] = 5;
  118.        
  119.         // now check
  120.         for(int i = 0; i < 10; ++i){
  121.             System.out.print(" " + dc1.data[i]);
  122.         }
  123.         System.out.println();
  124.         for(int i = 0; i < 10; ++i){
  125.             System.out.print(" " + dc2.data[i]);
  126.         }
  127.        
  128.         System.out.println();
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment