Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Analysis of Java in the deep copy and shallow copy     - CentOS yum install LAMP (Server)

- Use FirewallD build dynamic firewall (Linux)

- Nginx version of helloworld (Server)

- C language sorting instance (select, bubble, insert, binary, fast) (Programming)

- Cancel Root Command History under Linux (Linux)

- Wi-Fi hackers use to attack your seven methods (Linux)

- Oracle data row split multiple lines (Database)

- Use exp exported EXP-00091 error (Database)

- Linux system server network security management tips (Linux)

- Oracle RMAN-06023 and ORA-19693 errors (Database)

- Java reflection summary (Programming)

- Using DOS command to change UNIX administrator password (Linux)

- Oracle 11g How dataguard master repository to Oracle single instance data recovery (Database)

- Ubuntu 15.04 / 14.04 install Ubuntu After Install 2.6 (Linux)

- Setting Wetty do not need an account login command line operations (Linux)

- Can not empty the Recycle Bin to repair problems in Ubuntu 14.04 (Linux)

- A summary of Java multi-threaded programming - acquaintance multithreading (Programming)

- Linux SU command security Suggestions (Linux)

- Talk about the Linux folder permissions issue again (Linux)

- CentOS 7 source code to compile and install Nginx process record (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;
- Understand the security restore accidentally deleted critical system files (Linux)
- Redis application of Sina Weibo (Database)
- Android will save the picture to see the album and timely (Programming)
- Executable file format Explanation under Linux (Linux)
- 3 tips Linux command (Linux)
- Docker Basic and Advanced (Linux)
- Ubuntu 15.10 install the latest Arduino IDE 1.6.7 (Linux)
- Oracle 12c In-Memory Study (Database)
- PostgreSQL Stream Configuration (Database)
- Vmstat command Linux Performance Monitoring (Linux)
- CentOS 7 open ports (Linux)
- Linux find command usage summary (Linux)
- How to customize your Linux desktop: Gnome 3 (Linux)
- Detailed usage history command (Linux)
- Getting Started with Linux system to learn: how to check in a package is installed on Ubuntu (Linux)
- Linux Mint 17.2 64 bit installation Docker and management software seagull (Linux)
- Writing Better Bash build script 8 (Programming)
- How ONLYOFFICE collaborative editing document on Linux (Linux)
- Restrict console access to Linux servers to improve security (Linux)
- Linux commands to access the cheat sheet (Linux)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.