Shallow copy and deep copy of the Clone method in Java __java

Source: Internet
Author: User
Tags intl shallow copy

Package test;

Class OBJ implements cloneable{
	private int aint=0;
	public int Getaint () {return
		aint;
	}
	public void Setaint (int intl) {
		aint=intl;
	}
	public void Changeint () {
		this.aint=1;
	}
	public Object Clone () {
		object o=null;
		try{
			o= (Object) Super.clone ();
		} catch (Clonenotsupportedexception e) {
			e.printstacktrace ();
		}
		Return o.
	}
}

public class test{public
	static void Main (string[] args) {
		obj a=new obj ();
		obj b= (obj) a.clone ();
		B.changeint ();
		System.out.println ("A:" +a.getaint ());
		System.out.println ("B:" +b.getaint ());
	}


The results of the program running are: a:0
B:1

In actual programming, you will often encounter an object B from an existing object A that creates another one with the same state, and a modification of B does not affect the case of a, such as in prototype (prototype) mode, which requires a clone of an object instance. In the Java language, this is clearly not achieved by simple assignment operations, and Java provides a simple and effective clone () method to meet this requirement.

All classes in Java incorporate the object class by default, and a clone () method is provided in the object class. The purpose of this method is to return a copy of an object. This copy function returns a new object instead of an application. So how do you use this method? The following are steps for using the Clone () method.

1, the implementation of the Clone () class, the first need to integrate the Cloneable interface. The Cloneable interface is essentially an identity interface and does not have any interface methods.

2. Override the Clone () method in the object class in the class

3. Call the Super.clone () method in the Clone method. Regardless of the inheritance structure of the Clone class, Super.clone () invokes the Clone method of the Java.lang.Object class, either directly or indirectly.

4, the shallow copy of the reference point to the prototype object of the new clone.

Shallow copy and deep copy


The same problem occurs when the Java language overloads the Clone () method. This is OK when there are only some basic data types in the class, but when some objects are included in the class, a deep copy is required, and the implementation is done after the object calls the Clone method completes the replication. The Clone () method is then invoked on the properties of the Cabernet type in the object, which completes deep replication. Specific examples are as follows:

Package test;
Import java.util.Date;

Class OBJ implements cloneable{
	private date birth=new date ();
	Public Date Getbirth () {return
		birth;
	}
	public void Setbirth (Date birth) {
		This.birth=birth;
	}
	public void ChangeDate () {
		this.birth.setMonth (4);
	}
	Public Object Clone () {
		Obj o=null;
		try{
			o= (OBJ) Super.clone ();
			
		} catch (Clonenotsupportedexception e) {
			e.printstacktrace ();
		}
		Implement deep copy
		o.birth= (Date) This.getbirth (). Clone ();
		Return o.
	}
}

public class test{public
	static void Main (string[] args) {
		obj a=new obj ();
		obj b= (obj) a.clone ();
		B.changedate ();
		System.out.println ("A:" +a.getbirth ());
		System.out.println ("B:" +b.getbirth ());
	}


The result is: A:sat Mar 19:46:34 CST 2015
B:thu May 19:46:34 CST 2015

Ensure that the member variables of all the non basic types contained in the class implement the deep copy Object O=super.clone (); Perform a shallow copy first attr execute the following statement o.attr=this.getattr () for each object. Clone ();

Shallow copy and deep copy differences:

Shallow copy: All variables of the copied object contain the same value as the original object, while references to other objects of all objects still point to the original object. In other words, a shallow copy of the object being considered, without copying the object it references.

Deep copy: All variables of the copied object contain the same values as the original object, excluding those that refer to other objects. The variables that refer to other objects will point to the new object being copied, not the original referenced object. Objects that are referenced by a deeply copied object are copied again.

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.