1 protected native Object clone() throws CloneNotSupportedException;
1. Method modified by native keywords
The native keyword in java indicates that this method is a local method, [java native description]. Moreover, the execution efficiency of native modification methods is higher than that of non- native modifications.
2. The method is modified by protected
When a class overrides the clone() method, it needs to be modified into a public access modifier, so as to ensure that all other classes can access this method of this class.
3. Method throws CloneNotSupportedException exception
If a class wants to override the clone() method, it must implement the java.lang.Cloneable interface itself, otherwise a CloneNotSupportedException will be thrown.
2. The role of clone()
Note: The objects here specifically refer to complex types.
1. Simple = operation
We know that objects of complex types in Java are all reference types, and they often store the memory addresses of the objects. Therefore, we cannot simply assign the value operation as the = operator. We assign an object a to another object b, and we simply assign the memory address of object a to b so that both of them are pointing to the same memory address. The consequence of this is that modifications to one of the objects will affect the other object. The following figure shows:
Person p1 = new Person();Person p2 = p1;
2. clone()
Using the clone() method, you can quickly create a copy of an object, and the two objects point to different memory addresses. The following figure shows:
Person p1 = new Person();Person p2 = p1.clone();
3. shallow clone and deep clone1, shallow clone (shallow copy)
shallow clone refers to fields in the clone object itself, not the clone object. Only call super.clone(), just shallow clone. Although the copied object points to different memory addresses, the fields in the object still point to the same memory address as the previous object.
public class ShallowClone implements Cloneable { public String name; public int age; public Person person; public ShallowClone() { } public ShallowClone(String name, int age, Person person) { this.name = name; this.age = age; this.person = person; } @Override public ShallowClone clone() { ShallowClone c = null; try { c = (ShallowClone) super.clone(); return c; } catch (CloneNotSupportedException e) { e.printStackTrace(); } return c; } public static void main(String[] args) { Person p = new Person(); p.name = "p"; p.age = 10; ShallowClone c1 = new ShallowClone("Jim", 18, p); System.out.printf("before clone: c1 = %s, c1.person = %s/n", c1, c1.person); ShallowClone c2 = c1.clone(); System.out.printf("after clone: c2 = %s, c2.person = %s/n", c2, c2.person); }}Run main() output:
before clone: c1 = cre.sample.test.object.ShallowClone@558385e3, c1.person = cre.sample.test.Person@2dcb25f1after clone: c2 = cre.sample.test.object.ShallowClone@742808b3, c2.person = cre.sample.test.Person@2dcb25f1
In this way, the memory address of the ShallowClone object has changed, but the memory address of the Person field in the object has not changed;
2. deep clone (deep copy)
deep clone refers to the fields in the clone object itself and also the clone object.
/** * deep clone code example* Created by CreGu on 2016/6/9. */public class DeepClone implements Cloneable { public String name; public int age; public Person person; public DeepClone() { } public DeepClone(String name, int age, Person person) { this.name = name; this.age = age; this.person = person; } @Override public DeepClone clone() { DeepClone c = null; try { c = (DeepClone) super.clone(); c.person = person.clone(); return c; } catch (CloneNotSupportedException e) { e.printStackTrace(); } return c; } public static void main(String[] args) { Person p = new Person(); p.name = "p"; p.age = 10; DeepClone c1 = new DeepClone("Jim", 18, p); System.out.printf("before clone: c1 = %s, c1.person = %s/n", c1, c1.person); DeepClone c2 = c1.clone(); System.out.printf("after clone: c2 = %s, c2.person = %s/n", c2, c2.person); }}Run main() output:
before clone: c1 = cre.sample.test.object.DeepClone@558385e3, c1.person = cre.sample.test.Person@2dcb25f1after clone: c2 = cre.sample.test.object.DeepClone@742808b3, c2.person = cre.sample.test.Person@70535b58
In this case, the memory address of the DeepClone object has changed, but the memory address of the Person field in the object has also changed.
The above comprehensive analysis of the clone method of java object is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.