Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Analysis of Java in the deep copy and shallow copy     - Linux firewall rules example Extracts (Linux)

- CentOS use wget (Linux)

- Linux install Eclipse for C / C ++ Development (Linux)

- Java deserialization test (Programming)

- ActiveMQ5.10.2 version configuration JMX (Linux)

- Repair Raspbian Encountered a section with no Package (Linux)

- PL / SQL data types (Database)

- Examples of Python any parameters (Programming)

- Ubuntu 14.04 to install Blender 2.71 (Linux)

- Android using SVG vector graphics to create cool animation effects (Programming)

- phpinfo (): like phpinfo as a Python script (Programming)

- Oracle can not change the tablespace to backup mode in non-archive mode (Database)

- SSH configuration under Linux (Linux)

- Linux C source code (Ascii HexToBinary: Converts hexadecimal string format ASCII codes) (Programming)

- Use mysqldump MySQL database backup - Linux Shell Scripting (Database)

- Let Git variety of colors and custom log format output (Linux)

- Linux use glibc2 library and crypt () function to generate the user password (Programming)

- SLF4J Tutorial (Programming)

- JDK tools jstat (Linux)

- Getting Started with Linux system to learn: how to check in a package is installed on Ubuntu (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:      
 
- Loop arrays in C language is not easy to find a bug (Programming)
- Formatted output printf command (Programming)
- IP configuration under Linux (Linux)
- Vagrant build LNMP environment (Server)
- Java memory area (Programming)
- Ubuntu 14.10 / 14.04 / 12.04 How to install Kodi 14.0 RC3 (Linux)
- Introduces Linux kernel compilation system and compiler installation (Linux)
- Ubuntu 13.04 configure MyEclipse 10.7 Environment (Linux)
- Android in the event delivery and handling mechanism (Programming)
- Hadoop virtualization performance comparison and tuning experience (Server)
- To record command input under Linux (Linux)
- About Python default character set (Linux)
- Understanding the type in C ++ bitset (Programming)
- Firewall Configuration Red Hat Enterprise Linux 4 (Linux)
- Ubuntu 14.04 LTS NTFS partition can not access solution (Linux)
- How to Install Linux kernel 4.0 on Ubuntu / CentOS (Linux)
- MySQL import large amounts of data (Database)
- Redis configuration file interpretation (Database)
- OpenDJ installed on RHEL6 (Linux)
- CentOS 7 Configure logging (VirtualBox) (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.