The understanding of clone in Java

Source: Internet
Author: User

Interview often encounter clone of the relevant knowledge, today is finally a more thorough understanding of the clone! The concept of clone in Java everyone should be familiar with, it can make it very convenient to "make" a copy of the object, below to see how the clone mechanism in Java works?
1. Clone and copy
Suppose you now have a user object, user u1=new user ("U1001", "Jason", 25),
Often we will have this assignment user u2=u1, this time simply copy a bit reference,u2 and U1 all point to the same object in memory, so U2 or U1 an operation can affect each other. For example, if we change the value of the age field through the U2.setage () method, then U1 through the Getage () method is the value of the modified age field, which is obviously not what we would like to see. We want to get an exact copy of the U1, while the two do not affect each other, and we can use clone to meet our needs. User U2=u1.clone (), a new user object is generated and has the same property values and methods as the U1.

2. Shallow clone and Deep clone
How is clone done? Object does not know what to do when cloning an object, it simply executes the domain-to-domain copy, which is shallow Clone. So, here's the problem, in the case of user, it has a domain birthday not a basic type variable, but a reference variable that, after cloning, produces a new date type reference, It points to the same date object as the corresponding field in the original object, so that the Clone class shares part of the information with the original class, which is obviously unfavorable, as shown in the procedure:


At this point we need to do deep clone, special processing of those non-basic domains, such as the birthday in this example. We can redefine the Clone method and do special processing for birthday, as shown in the following code:

Java code
    1. Class User implements Cloneable
    2. {
    3. Public Object Clone () throws Clonenotsupportedexception
    4. {
    5. User cloned = user super.clone ();
    6. Cloned.birthday = (Date) hireday.clone ()
    7. return cloned;
    8. }
    9. }

3. The protection mechanism of the Clone () method in Object Clone () is declared as protected, there is a certain reason, in order to use the user class as an example, through the declaration of protected, you can guarantee that only the user class inside to "clone" the user object, The principle can refer to my previous study notes on public, protected, private.

4. Clone () method using the Clone () method is relatively simple to use, pay attention to the following points: A. When to use shallow clone, when to use deep clone, this main look at the specific object of what the nature of the domain, the basic type or reference Variableb. The class to which the object calling the Clone () method belongs must implements the Clonable interface, or clonenotsupportedexception is thrown when the Clone method is called.

The understanding of clone in Java

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.