Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Analysis of Java in the deep copy and shallow copy     - Linux server disk expansion and Oracle tablespace file migration operations (Database)

- Justniffer installed on Ubuntu 15.04 (Linux)

- Sleuth Kit: used to analyze a disk image and restore files open source forensics tools (Linux)

- Xshell configure SSH free password (Server)

- Installation of network monitoring ntopng under CentOS 6.4 (Linux)

- CentOS install video converter FFmpeg and cutting tools segmenter (Linux)

- Ubuntu 12.04 installation DHCP Server (Server)

- Android Custom View step (Programming)

- How do you access Dropbox Linux command line (Linux)

- Ubuntu 14.04 to install Blender 2.71 (Linux)

- Linux Kernel 4.2 Installation Instructions (Linux)

- Snort build a secure Linux server (Linux)

- Security Knowledge: redirection command application security (Linux)

- Print Linux system error codes (Linux)

- MySQL and MariaDB traditional master-slave cluster configuration (Database)

- CentOS iptables firewall enabled (Linux)

- Linux text processing tool of sed (Linux)

- Create RAID 1 (mirroring) with two disks (Linux)

- Lua regex (string function) (Programming)

- Linux network security strategy (Linux)

 
         
  Analysis of Java in the deep copy and shallow copy
     
  Add Date : 2018-11-21      
         
         
         
  First we look at the definition of shallow vs. deep copy
    Shallow copy: just copy an object, pointing to an array of other objects inside the object exists or reference are not copied

    Deep copy: reference object within the object are copied

    To better understand the difference between them we assume that an object A, which contains 2 objects Object A1 and A2

 

    When an object A shallow copy, object B but object is obtained A1 and A2 were not copied



    A deep copy of the object, while the object B obtained A1 and A2 together with their references are also copied



    After understanding the deep copy and shallow copy, let's look at the deep and shallow copy copy Java implementation. The java.lang.Object clone () method before the default is to return a copy of the object. So if you use the clone () method to implement a deep copy, we must () method to achieve the particular clone each object. When the object hierarchy complicated when doing so is not only difficult but also a waste of time and error-prone, especially sometimes you only need a deep copy of the object at the same time you also shallow copy, you will find that writing this clone () method is not really a good solution.

    Then in addition to clone () method, we can also how to achieve it? The answer is serialized, implementation steps and the idea is to copy objects into the output byte array, then use ObjectInputStream convert a new object. Here is the code
public static Object copy (Object oldObj) {
    Object obj = null;
    try {
        // Write the object out to a byte array
        ByteArrayOutputStream bos = new ByteArrayOutputStream ();
        ObjectOutputStream out = new ObjectOutputStream (bos);
        out.writeObject (oldObj);
        out.flush ();
        out.close ();
  
        // Retrieve an input stream from the byte array and read
        // A copy of the object back in.
        ByteArrayInputStream bis = new ByteArrayInputStream (bos.toByteArray ());
        ObjectInputStream in = new ObjectInputStream (bis);
        obj = in.readObject ();
    } Catch (IOException e) {
        e.printStackTrace ();
    } Catch (ClassNotFoundException cnfe) {
        cnfe.printStackTrace ();
    }
    return obj;
}
     
         
         
         
  More:      
 
- Deploy Apache Spark cluster environment in Ubuntu (Server)
- Get basic information about Linux server script (Server)
- Detailed PHP code optimization [section] (explain reasons) (Programming)
- Install minimize RHEL / CentOS 7 some things need to do (Linux)
- Linux environmental performance data acquisition system (Linux)
- Binary tree traversal: the first sequence in order preorder recursive and non-recursive and traversal sequence (Programming)
- Oracle 11g logical standby achieve BI needs (Database)
- Java call by value and by reference (Programming)
- According to the national position on how to block traffic in Linux (Server)
- Linux Command - ps: a snapshot of the current process (Linux)
- CentOS 5.5 kernel upgrade installation iftop (Linux)
- To install Emacs under CentOS 6.5 (Linux)
- MyCAT log analysis (Database)
- Use ISO document production OpenStack used CoreOS mirror (Linux)
- Open container cluster management system architecture and components introduced Kubernetes (Server)
- Linux FAQ - How to fix tar:Exitingwith failure status due to previous errors (Linux)
- Elixir: the future of programming languages (Programming)
- To compile and install OpenCV-2.3.1 FFmpeg-2.1.2 under CentOS (Linux)
- Graphics of Java Tools (Programming)
- Ubuntu 14.10 Install Ubuntu Touch Music App 2.0 (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.