Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Analysis of Java in the deep copy and shallow copy     - Linux System Getting Started Learning: The Linux command line burn ISO or NRG image to DVD (Linux)

- Intel Graphics Installer installation on Ubuntu 15.10 (Linux)

- JavaScript function part (Programming)

- CentOS6.5 install SVN & visual management tools iF.SVNAdmin (Server)

- Oracle Database Performance Optimization of memory disk (Database)

- Linux system monitoring, top command of the diagnostic tool Detailed (Linux)

- Custom Android UI template Comments (Programming)

- Clojure programming languages: take full advantage of the Clojure plug-in Eclipse (Programming)

- Quick Install software RAID on Linux (Linux)

- Ubuntu and derived versions of the user how to install G Mic 1.5.8.5 (Linux)

- Fedora 20, Fedora 19, CentOS 6 and RHEL6 users how to install Wine 1.7.15 (Linux)

- ethtool command Detailed (Linux)

- Linux dd command make U disk boot disk (Linux)

- Cobbler automatic mass deployment of CentOS 6 and CentOS 7 (Linux)

- 20 open source / commercial Linux server management control panel (Server)

- SHELL script to use anti SSH brute force and vsftpd (Linux)

- A well-known but rarely used skills: object pool (Programming)

- C ++ inline functions (Programming)

- On Android running ClojureScript (Linux)

- Spring + Log4j + ActiveMQ remote logging - Analysis of combat (Server)

 
         
  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:      
 
- TL-WR703N to install OpenWrt process notes (Linux)
- Those functions under Linux you do not know the df command (Linux)
- Ubuntu Gnome and Fedora 22 Gnome desktop, extended to achieve global menu (Linux)
- Achieve single backup of MySQL by innobackupex (Database)
- CentOS7 management of systemd (Linux)
- Build Golang development environment configuration on Ubuntu 14.04 (Linux)
- How to use GRUB2 files directly from the hard disk to run ISO (Linux)
- Linux system using the command line shutdown or restart (Linux)
- Linux Open coredump (Linux)
- Installation and use of Linux Sniffer tool Tcpdump (Linux)
- Java Builder mode (Programming)
- Oracle 11g modify MEMORY_TARGET (Database)
- Use 3G wireless network card under Linux (Linux)
- MySQL combination Git achieve differential backups can be used in the production environment (Database)
- How to install the Linux text editor Atom 0.124.0 (Linux)
- Install Apache streaming media services on CentOS 6.4 (Server)
- Elaborate .NET Multithreading: Thread Pool (Programming)
- Linux system security reinforcement system by masquerading method (Linux)
- CentOS installation pycurl (Linux)
- mydumper installation error process (Database)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.