Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Analysis of Java in the deep copy and shallow copy     - Android determine the device network connection status, and determine the connection (Programming)

- After reloading the cinder-volume OpenStack not start properly (Server)

- What Java 8 fastest garbage collector (Programming)

- Security measures under Unix multi-user operating system (Linux)

- Automatic and Manual Proxy Settings Switch GNOME Shell Extension Proxy Switcher Recommend (Linux)

- Java proxy use and proxy principle (Programming)

- Learning OpenCV: (VS2010-openCV2.4.3-win7 configuration instructions) (Linux)

- Network security system (Network)

- Shell Scripting Basics (Linux)

- Linux Operating System Security Study (Linux)

- Java memory area (Programming)

- Deb package installation method under ubuntu (Linux)

- You know the difference between URL, URI and URN among you (Linux)

- 20 Top Linux commands (Linux)

- Python is not C (Programming)

- Large computer network security policy Experience (Linux)

- C language - Traverse pci device (Programming)

- Java interface and inheritance (Programming)

- ARM constant expression (Programming)

- Download Manager uGet 2.0 installed in Debian, Ubuntu, Linux Mint and Fedora (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:      
 
- VMware6 achieve nat Internet (Linux)
- Ubuntu 14.04 forget solution root password (Linux)
- How to view the Linux QPS (Linux)
- Java in the inverter and covariance (Programming)
- To modify the existing user ID and comments GPG key (Linux)
- Ubuntu 14.04.02 LTS startup items erroneous writing / dev / sda1 (win 7 loader) Repair (Linux)
- ActiveMQ configuration Getting Started Tutorial (Server)
- How to use SHA256 checksum files download (Linux)
- Phoenix agents use P2P WebRTC development (Programming)
- How do you know your public IP terminal in Linux (Linux)
- Vagrant build LNMP environment (Server)
- Linux module mechanism of (Programming)
- Distributed File System using MogileFS (Linux)
- Installing PHP Memcache extension under Linux (Server)
- Ubuntu 14.04 / 12.04 subscribe users to install software Liferea 1.10.10 (Linux)
- Using PHP MySQL library (Programming)
- RHEL6.5 install the latest version of Vim and increase support for the Python2.7.5 (Linux)
- Installation and configuration under Linux SVN server - Backup - Recovery - Scheduled Tasks (Server)
- Samba public folder permissions (Server)
- Keepalived + Nginx Installation and Configuration (Server)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.