A brief talk on cloning the difference between close () and assignment reference in Java _java

Source: Internet
Author: User
Tags garbage collection

Student Class Student:

Cloning clone clone package;

/* To clone must implement this excuse: cloneable, to mark this object can be cloned
 cloneable: This class implements the Cloneable interface to indicate that the Object.clone () method can legitimately replicate the class instance by field. 
 This interface is a markup interface that tells us that the class that implements the interface can replicate the object.
 *
/public class Student implements cloneable {
	private String name;
	private int age;

	Public Student () {
	} public

	Student (String name, int age) {
		super ();
		this.name = name;
		This.age = age;
	}

	Public String GetName () {return
		name;
	}

	public void SetName (String name) {
		this.name = name;
	}

	public int getage () {return age
		;
	}

	public void Setage (int age) {
		this.age = age;
	}

	Overriding the Clone method child column to invoke
	@Override
	protected Object Clone () throws Clonenotsupportedexception {
		return Super.clone ();
	}


Test Clone Student Class:

 package clone clone; /* protected void Finalize (): This method is called by the object's garbage collector when the garbage collector determines that there are no more references to the object.
 For garbage collection, but when recycling is not determined.
 * Protected Object Clone (): Creates and returns a copy of this object. 
 * A: Rewrite the method * * Cloneable: This class implements the Cloneable interface to indicate that the Object.clone () method can legitimately replicate the class instance by field.
 * This interface is a markup interface that tells us that the class that implements the interface can replicate the object. */public class Studentclone {public static void main (string[] args) throws Clonenotsupportedexception {//Create student Object St
		udent s = new Student ();
		S.setname ("Brigitte");
		
		S.setage (27);
		Clone Student Object obj = S.clone ();
		Student s2 = (Student) obj;
		
		System.out.println ("---------");
		System.out.println (S.getname () + "---" +s.getage ());
		
		System.out.println (S2.getname () + "---" +s2.getage ());
		The previous practice Student S3 = s;
		System.out.println (S3.getname () + "---" +s3.getage ());
		
		System.out.println ("---------");
		In fact there is a difference between the S3.setname ("Liu Yi");
		S3.setage (30);
		System.out.println (S.getname () + "---" +s.getage ());
		System.out.println (S2.getname () + "---" +s2.getage ()); System.out.prIntln (S3.getname () + "---" +s3.getage ());
		
		System.out.println ("---------");
		Change the Clones?
		S2.setname ("Name of clone Change");
		S2.setage (66);
		System.out.println (S.getname () + "---" +s.getage ());
		System.out.println (S2.getname () + "---" +s2.getage ());
		
		System.out.println (S3.getname () + "---" +s3.getage ()); Which means assigning a reference to an object to a reference, when this reference makes a change, the object that the same reference points to will change, but the clone does not change,//The cloned object changes some of his properties, so it does not affect the change of the value of other objects}} 

The above is a small series for you to bring a brief discussion of cloning close () and assignment reference in Java all the contents of the difference, I hope to help you, a lot of support cloud Habitat Community ~

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.