Java Study Notes-you may not know the knowledge about object replication and reference.

Source: Internet
Author: User

Java Study Notes-you may not know the knowledge about object replication and reference.

1. private, protected, and static cannot be used to modify interfaces.

2. When processing basic data types (such as int, char, and double), java uses the value-based transmission method for execution, and Other types are passed by reference. In addition to the reference transfer during function calls, an object also uses reference transfer when "=" is assigned a value.

3. How to copy objects in java instead of references. Example:

 

class Obj implements Cloneable{private int a=0;public int getA(){return a;}public void setA(int b){this.a=b;}public void changedA(){this.a=1;}public Object clone(){Object o=null;try{0=(Obj)super.clone();}catch(CloneNotSupportedException e){e.printStatckTrace();}return o;}}public class TestRef{public static void main(String args[]){Obj a= new Obj();Obj b=(Obj)a.clone();b.changedA();System.out.println("a:"+a.getA());System.out.println("b:"+b.getA());}}

The program running result is:

 

A: 0

B: 1

Therefore, to assign a value to a class, you must implement the Cloneable interface. Steps:

(1) clone must first inherit the Cloneable interface. The Cloneable interface is essentially an identity interface (there is no method, that is, an empty Interface)

(2) rewrite the clone () method of the Object class in the class.

(3) Call super. clone () in the clone () method ().

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

When a class contains complex object attributes other than basic data, the clone () method must be rewritten to perform deep replication on attributes of non-basic types. Example:

 

Class Obj implements Cloneable {private Date birthday = new Date (); private int a = 0; public int getA () {return a;} public void setA (int B) {this. a = B;} public void changedA () {this. a = 1 ;}public Date getDate () {return birthday () ;}public void setDate (Date date) {this. birthday = date;} public void changeDate () {this. birthday. setMonth (4);} public Object clone () {Object o = null; try {0 = (Obj) super. clone ();} catch (CloneNotSupportedException e) {e. printStatckTrace ();} // implements deep replication o. birthday = (Date) this. getDate (). clone (); return o ;}} public class TestRef {public static void main (String args []) {Obj a = new Obj (); Obj B = (Obj). clone (); B. changedA (); System. out. println ("a:" +. getA (); System. out. println ("B:" + B. getA ());}}


 

Differences between shallow replication and deep replication:

 

Shallow copy: all variables of the Copied object contain the same value as the original object, and all references of other objects still point to the same value of the original object, all references to other objects still point to the original objects.

Deep replication: all the variables of the Copied object contain the same value as the original object and get out the variables that reference other objects. The variables that reference other objects will point to the new objects to be copied, instead of the original objects that have been referenced.

 

 

 

 

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.