Java deep copy of an object copy back

In java development, sometimes we need to copy the object, and modify the copy to ensure that the resulting object will not affect the original object.
So, some people might write code like the following:

public class CloneTest {
    public static void main(String[] args)  {
        JSONObject paramJson= new JSONObject();
        paramJson.put("age",26);
        paramJson.put("name","lin");
        JSONObject cloneJson= paramJson;
        cloneJson.put("test",27);
        System.out.println("paramJson:"+paramJson.toString());
        System.out.println("cloneJson:"+cloneJson.toString());
    }
}

Operating results as follows:

paramJson:{"age":27,"name":"lin"}
cloneJson:{"age":27,"name":"lin"}

Found objects to get the assignment change, the original object has changed.
The value of the transmission and passed by reference already mentioned, the
reference or parameter passing type assignment, when a new object is changed, the original object will change.
So, how to ensure that the new object modification will not affect the original copy obtained by the object?

Deep and shallow copy copy back back

Copy the object, modify the new object does not affect the original object. This is actually a deep copy back.
The new object modification will affect the resulting copy the original object, it is a shallow copy back.

From the reference value and understanding, the following concepts:
1. shallow copy: the original or the original object reference array directly assigned to the new object, a new array, a new object / reference array is just a primary object
2. deep copy: Create a new objects and arrays, the original "value" attribute of the object (all the elements of the array) copied, the "value" rather than the "reference"

In the superclass Object java, a clone () method, the default shallow copy back.

protected Object clone()
                throws CloneNotSupportedException

So, how deep copy of it back?

Deep beat back the realization

apache-commons package has several tools can use a deep copy back.
1.BeanUtils.

public class DeepCloneTest {
    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
        JSONObject paramJson= new JSONObject();
        paramJson.put("age",26);
        paramJson.put("name","lin");
        JSONObject cloneJson= new JSONObject();
        BeanUtils.copyProperties(cloneJson,paramJson);
        cloneJson.put("test",27);
        System.out.println("paramJson:"+paramJson.toString());
        System.out.println("cloneJson:"+cloneJson.toString());
    }
}

operation result:

paramJson:{"name":"lin","age":26}
cloneJson:{"test":27}

2.SerializationUtils. Object serialization requirements must implement the interface.

public class DeepCloneTest {
    public static void main(String[] args) {
        JSONObject paramJson= new JSONObject();
        paramJson.put("age",26);
        paramJson.put("name","lin");
        JSONObject cloneJson=SerializationUtils.clone(paramJson);
        cloneJson.put("age",27);
        System.out.println("paramJson:"+paramJson.toString());
        System.out.println("cloneJson:"+cloneJson.toString());
    }
}

operation result:

paramJson:{"name":"long","age":26}
cloneJson:{"name":"long","age":27}

Guess you like

Origin www.cnblogs.com/expiator/p/11135968.html