Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Analysis of Java in the deep copy and shallow copy     - Linux ldconfig command (Linux)

- RedHat Performance Tuning (Server)

- Kibana Apache Password Authentication (Server)

- Linux command Detailed chpasswd bulk edit user password (Linux)

- C language preprocessor command (Programming)

- Formatted output printf command (Programming)

- Ubuntu 14.10 How to install office suite Calligra Suite 2.8.7 (Linux)

- Python uses multi-process pool (Programming)

- Nginx high concurrency optimization ideas (Server)

- Depth understanding of the use of Spring Redis (Programming)

- Raspberry Pi configuration wireless hotspot (Linux)

- CentOS 7 virt-manager can not connect a local hypervisor (Linux)

- Oracle set and remove columns unavailable (Database)

- Java reflection by calling the class method (Programming)

- JavaScript prototype and the prototype chain (Programming)

- Smooth upgrade to OpenSSH 6.1 Procedure (Linux)

- stat - Get more information than ls (Linux)

- Ubuntu 14.04 build Gitlab (Linux)

- Vim useful plugin: vundle (Linux)

- MySQL log from the library than the main library (Database)

  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;
- JavaScript closures and the scope chain (Programming)
- Eclipse installs support for Java 8 (Linux)
- The Linux C truncate function clears the file notes (Programming)
- Understand the profound discussion of some of the options for wireless encryption (Linux)
- Xshell key authentication mechanism using a remote login Linux (Linux)
- How to use OpenVPN and PrivacyIDEA build two-factor authentication for remote access (Server)
- RHEL5.8 physical opportunities to Read-only file system (Linux)
- C data types is how it is supported by most computer systems (Programming)
- Django1.5 Custom User Model (Programming)
- Empty password Linux operating system (Linux)
- Text editing and viewing text Linux command (Linux)
- Linux vi command list (Linux)
- Linux use logs to troubleshoot (Linux)
- Installation on Ubuntu class Winamp audio player Qmmp 0.9.0 (Linux)
- xCAT Installation Kit (Linux)
- Android graphic introduction NDK installation and simple jni demo implementation (Linux)
- Ubuntu 14.04 to install file editor KKEdit 0.1.5 version (Linux)
- iptables allow only specific ip address to access the specified port (Linux)
- FreeBSD install Gnome 3 Desktop (Linux)
- How to install Laravel PHP framework on CentOS 7 / Ubuntu 15.04 (Server)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.