Home > Java > javaTutorial > body text

How is object cloning implemented in Java?

WBOY
Release: 2024-04-11 21:18:01
Original
414 people have browsed it

Clone of objects in Java is implemented through the Cloneable interface. You must override the clone() method and explicitly throw CloneNotSupportedException. Cloning can be divided into deep copy and shallow copy: 1. Deep copy creates a complete copy of the object, including variable fields; 2. Shallow copy only copies the reference, and the original object and the copy share the same data.

How is object cloning implemented in Java?

#How is object cloning implemented in Java?

Introduction

In Java programming, cloning is used to create a copy of an object without modifying the original object. Cloning of objects can be used in various scenarios, such as creating multiple instances of an object, passing a copy of an object to avoid modifying the original object, and implementing deep and shallow copies.

Implementing Clone

Clone in Java is implemented by the Cloneable interface. Any class that wishes to implement cloning must implement this interface and override the clone() method. The clone() method returns a clone of the object and must explicitly throw CloneNotSupportedException if the class does not support cloning.

Deep copy and shallow copy

When cloning an object, two types of copies can be made: deep copy and shallow copy.

  • Deep copy: Creates a completely new copy of an object, including all of its fields. Even mutable fields are copied, so any changes to the copy will not affect the original object.
  • Shallow copy: Only the references to the object and its mutable fields are copied. The original object and the copy point to the same underlying data. Therefore, any changes to the copy also affect the original object.

Practical case

Consider the following Student class, which has three fields: name, age and address:

public class Student implements Cloneable {
  private String name;
  private int age;
  private Address address;

  @Override
  public Object clone() throws CloneNotSupportedException {
    return super.clone();
  }

  // 省略getter 和 setter 方法
}
Copy after login

Address The class is also cloneable:

public class Address implements Cloneable {
  private String street;
  private String city;
  private String state;

  @Override
  public Object clone() throws CloneNotSupportedException {
    return super.clone();
  }

  // 省略getter 和 setter 方法
}
Copy after login

Now, consider the following code:

// 创建原始 Student 对象
Student originalStudent = new Student();
originalStudent.setName("John Doe");
originalStudent.setAge(21);
Address originalAddress = new Address();
originalAddress.setStreet("123 Main Street");
originalStudent.setAddress(originalAddress);

// 克隆原始Student对象
Student clonedStudent = (Student) originalStudent.clone();

// 修改克隆对象的字段
clonedStudent.setName("Jane Doe");
clonedStudent.setAge(22);
Address clonedAddress = clonedStudent.getAddress();
clonedAddress.setStreet("456 Elm Street");

// 输出原始和克隆对象
System.out.println("Original Student:" + originalStudent);
System.out.println("Cloned Student:" + clonedStudent);
Copy after login

The output will show:

Original Student:Student{name='John Doe', age=21, address=Address{street='123 Main Street', city=null, state=null}}
Cloned Student:Student{name='Jane Doe', age=22, address=Address{street='456 Elm Street', city=null, state=null}}
Copy after login

In this In this case, since the Address class is cloneable, this is a deep copy. When you modify a cloned object's address field, it does not affect the original object's address field.

The above is the detailed content of How is object cloning implemented in Java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template