简体   繁体   中英

About the Java interface Cloneable

I have a problem about the Interface Cloneable.That is:

  1. First I define a class named User.

    public class User{ public String name; public int age; public User(String name,int age){ this.name=name; this.age=age; } }

  2. Then I define a class named Account that include nested object User and implement the Cloneable.

    public class Account implements Cloneable{ public User user; public int num; public Account(User user,int num){ this.user=user; this.num=num; } @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } }

  3. Finally,I create a Test class named A to verify whether the user from original object is equal to the user from cloned object,and print the name and age of user.The result make me confused because some tutorials demonstrate the object references is just shallow copy and both the original object and cloned object use the same memory address.

    public class A { public static void main(String[] args) throws CloneNotSupportedException { User user=new User("alan", 24); Account a1=new Account(user, 0); Account a2=(Account) a1.clone(); System.out.println(a1.user==a2.user); System.out.println(a1.user.equals(a2.user)); System.out.println("a1.user.name="+a1.user.name); System.out.println("a1.user.age="+a1.user.age); System.out.println("a2.user.name="+a2.user.name); System.out.println("a2.user.age="+a2.user.age); a2.user=new User("mary", 20); System.out.println(a1.user==a2.user); System.out.println(a1.user.equals(a2.user)); System.out.println("a1.user.name="+a1.user.name); System.out.println("a1.user.age="+a1.user.age); System.out.println("a2.user.name="+a2.user.name); System.out.println("a2.user.age="+a2.user.age); } }

  4. The result.

    true true a1.user.name=alan,a1.user.age=24 a2.user.name=alan,a2.user.age=24 false false a1.user.name=alan,a1.user.age=24 a2.user.name=mary,a2.user.age=20

I think the user from original object is same with cloned object.And both user have same name and age after changed.Forgive my bad English,thx.

You are calling Object 's clone . The Javadoc states that this method performs shallow copy :

The method clone for class Object performs a specific cloning operation. First, if the class of this object does not implement the interface Cloneable, then a CloneNotSupportedException is thrown. Note that all arrays are considered to implement the interface Cloneable and that the return type of the clone method of an array type T[] is T[] where T is any reference or primitive type. Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned. Thus, this method performs a "shallow copy" of this object , not a "deep copy" operation.

That's the reason why a1.user==a2.user after the cloning, since both refer to the same User object. However, a1 != a2 .

some tutorials demonstrate the object references is just shallow copy and both the original object and cloned object use the same memory address

I'm not sure what you are confused about. Your code demonstrates the same thing (as stated in the Javadoc) - the members of the cloned object are shallow copies. Each member of the cloned object refers to the same object as the respective member of the original object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM