Comparison of the ‘Object Assign’ framework and object cloning

Comparison of the "Object Assign 'framework in the Java Class Library and the object clone Overview: In Java, there are two commonly used ways to create and cloning objects, which use the 'Object Assign' framework and object cloning.This article will compare and discuss these two methods, including their principles, methods and applicable scenarios. 1. Object assign 'framework: 'Object assign' is a feature introduced by Java 8, which is used to copy the attribute value of one object to another.Its principle is to copy the attribute value through reflection. Instructions: The following is an example code that uses the "Object Assign 'framework to replicate the object attribute: import java.util.Objects; public class Person { private String name; private int age; // constructor, getters and setters @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } } public class Main { public static void main(String[] args) { Person person1 = new Person("Alice", 20); Person person2 = new Person(); Objects.requireNonNull(person1, "Person object cannot be null"); if (person2 != null) { Objects.requireNonNull(person2, "Person object cannot be null"); // Copy the attribute value of Person1 to Person2 person2 = (Person) ObjectAssign.copyNonNullProperties(person1, person2); System.out.println(person2); } } } Applicable scene: The 'Object Assign' framework is very suitable for scenes that need to copy attribute values between two objects, especially when there are a lot of attributes in the object.It can avoid manually writing a lengthy attribute assignment code to improve development efficiency. advantage: 1. Simplify the process of attribute copying, and avoid writing a large amount of lengthy attribute assignment code; 2. Support the condition of the attribute, it can easily selectively copy the attribute; 3. It can automatically handle the empty object and throw an exception. shortcoming: 1. Reflex has a certain impact on performance, which may cause the performance of the replication operation to be slightly lower than the manual writing attribute assignment code; 2. For complex objects, you may need to write an additional logic to define the method of copying attributes. Second, object cloning: Object cloning is a concept in Java, which means creating a new object similar to the original object but completely independent.Object cloning can be implemented by implementing the Cloneable interface and covering the Clone () method. Instructions: The following is a sample code that uses object cloning to implement object cloning: public class Person implements Cloneable { private String name; private int age; // constructor, getters and setters @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } } public class Main { public static void main(String[] args) { try { Person person1 = new Person("Alice", 20); Person person2 = (Person) person1.clone(); System.out.println(person2); } catch (CloneNotSupportedException e) { e.printStackTrace(); } } } Applicable scene: Object cloning is suitable for creating a new object similar to the original object but completely independent.It can be used to create prototype mode, a copy of the object quickly. advantage: 1. Creating object copy is very efficient, performance is better than using reflected copy attribute values; 2. You can customize the logic of the CLONEable interface and covering the clone () method. shortcoming: 1. You need to manually implement the Cloneable interface and cover the clone () method; 2. The clone () method is shallow copy, and the reference is reference, not the creation of a new object; 3. Object cloning may cause unexpected problems, such as shallow copies, cloning of singles objects, etc. Summarize: 'Object Assign' framework and object cloning are commonly used in Java.They all have their own advantages and disadvantages and applicable scenarios.If it is just a simple attribute copy and no other complex objects inside the object, you can choose to use the 'Object Assign' framework.If you need to create a similar but completely independent new object and need to customize the logic of copying, you can choose to use the object clone.In specific applications, developers can choose the appropriate cloning method according to demand and performance requirements.