Personal summary of Java clone and deep replication and shallow replication

Source: Internet
Author: User
Tags date1 modifier shallow copy
Comparison of 1.clone () and "="

An assignment in a primitive type variable such as int a = 1;int B = A;a has the same value as B, changing the value of a or B does not affect each other.

But between objects:

Java.util.Date date1 = new Java.util.Date (); java.util.Date date2 = Date1;

Date1 and Date2 point to the same object, pointing to the same storage space. Just like Xiaoming's nickname is Mingming, Xiaoming and Ming all point to the same person. You can change the object by changing the value of Date1 or Date2.

Using clone is different from the reference replication above.

Java.util.Date date1 = new Java.util.Date (); Java.util.Date date2 = (java.util.Date) date1.clone;

Date1 and Date2 point to different objects, but the data is the same, like two people with the same clothes. The operations between Date1 and date2 do not affect each other, they have their own storage space. Visible, a new object is date3 by Date1.clone ().

public class Test {public

	static void Main (string[] args) {
		//TODO auto-generated method stub
		Java.util.Date Date1 = new Java.util.Date ();
		Java.util.Date date2 = date1;
		Java.util.Date date3 = (java.util.Date) date1.clone ();
		System.out.println ("date1 = = date2?") + (Date1 = = Date2));
		System.out.println ("date1 = = date2?") + (Date1 = = Date3));
		
	}

Output result: Date1 = = Date2 true (using "=")
Date1 = = Date3 False (using Clone ())

2.clone () and cloneable interface. All classes inherit the object class by default, and the object class is defined with the Clone () method, the return type: object, modifier is protect, and subclasses are available. Therefore, you cannot directly use the Clone method on a variable of a class (such as defining a class student, declaring a variable astudent and creating an object, but not using Astudent.clone ()). Method of implementing Clone (): The modifier is changed to public by means of overriding the clone in the class. In addition, must also implement cloneable interface, otherwise it will throw an exception (detailed look at the summary of others http://onlylove.iteye.com/blog/265113, my level is not good, learn from). The date class above can use the Clone () method directly because the cloneable is implemented by default, and the Arraylist,calendar class can be used directly.

Class Student implements cloneable {
	int number;
	String name;
	
	@Override Public
	Object Clone () throws clonenotsupportedexception{return
		Super.clone ();
	}
The Student class implements the interface, with a clone method. 3.clone () shade copy. In a class, you do not need to consider shading if the data fields are all basic types.

If the data field contains objects, consider the problem of shading.

Class Student implements Cloneable {
int number;
String name;
Java.util.Date Date;

    @Override Public
	Object Clone () throws clonenotsupportedexception{return
		Super.clone ();
	}

This class has objects of the date class, and if you use the above method to implement clone (), a variable of the base type is still implemented as clone, but the date1 of the type object is only a reference. Student student1 = new Student (); Student Student2 = Student1.clone (); comparison: Student1.number = = Student2.number false s Tudent1.name = = Student2.name False
Student1.date = = Student2.date true
This is a shallow copy, and date does not implement cloning, but rather copies the reference. Equivalent to "=". At this point, as summarized in 1 above, date Student1 and Student2 point to the same object. deep copy depth replication is the case where date is not cloned in shallow replication. Override the Clone () method of the student class above:

Class Student implements cloneable {
	int number;
	String name;
	Java.util.Date Date;

	@Override Public
	Object Clone () throws Clonenotsupportedexception {
		Student studentclone = (Student) Super.clone ();
		Studentclone.date = (java.util.Date) this.date.clone ();
		Return Studentclone
	}
}
Now
Student student1 = new Student (); Student Student2 = Student1.clone (); comparison: Student1.number = = Student2.number false s Tudent1.name = = Student2.name False
Student1.date = = Student2.date false implements a deep copy of this class. Student1 and Student2 date point to a different object. (If there is a mistake, please correct me ~)
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.