Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Analysis of Java in the deep copy and shallow copy     - VMware difference in three network connection (Linux)

- Using monitoring tool dsniff (Linux)

- Linux system performance monitoring with Nmon (Linux)

- MySQL5.6.17 compiler installation under CentOS (Database)

- MySQL partition table Comments (Database)

- Linux install Maven and SVN client (Linux)

- Getting Started with Linux system to learn: how to get the process ID (PID) in the script (Linux)

- Red Hat Enterprise Linux configuration VNC multi-user access methods (Linux)

- Android timer use (Programming)

- Linux System Getting Started Learning: Change the name of the network interface on CentOS7 (Linux)

- Android gets the global process information and the memory used by the process (Programming)

- Mutt - an email client that sends messages through the terminal (Linux)

- Java Learning: elegant string (Programming)

- Proper use Core Data multithreaded 3 ways (Programming)

- fcntl file locking function add (Programming)

- RedHat command line and graphical interface switching (Linux)

- RedHat Redis Linux installation (Database)

- Delay for the specified IP port analog network to send and receive packets on Linux (Linux)

- ORA-12154: TNS: could not resolve the connect identifier specified solve (Database)

- Use smem visual display Linux memory usage (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:      
 
- Git and GitHub use of Eclipse and Android Studio (Programming)
- C ++ inline functions (Programming)
- Source code compiled by the installation program under Linux (Linux)
- Linux kernel programming parameter passing between modules and function calls (Programming)
- Oracle 12c R2 new feature dbca command to create a standby database (Database)
- C ++ containers (Programming)
- Open source backup software installed on Ubuntu Systemback 1.6.201 (Linux)
- How x2g0 install Remote Desktop on Linux VPS (Server)
- How to modify the SQL Server auto-increment value and the corresponding solution (Database)
- Linux server alarms using Java (Server)
- DELL D630 Wireless LAN Driver Installation CentOS6 (Linux)
- See how --nand flash timing diagram of a read operation Comments (Programming)
- Whisker Menu 1.4.3 Install menu Linux (Linux)
- Linux ls command (Linux)
- jQuery get value drop-down list and select text (Programming)
- Qt shared memory interprocess communication (Programming)
- Ubuntu 14.04 solved using MyEclipse 10.7 flash back (Linux)
- Bash command substitution (Programming)
- Teach you how to protect the Linux operating system security tips (Linux)
- Java 8 Lambda principle analysis (Programming)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.