java中三種拷貝方法舉例總結(jié)
前言
在Java編程中,理解深拷貝(Deep Copy)、淺拷貝(Shallow Copy)和引用拷貝(Reference Copy)是非常重要的。這三種拷貝方式涉及對象復制和內(nèi)存管理。以下是對它們的詳細解釋:
1. 引用拷貝(Reference Copy)
引用拷貝是最簡單的一種拷貝方式。它只復制對象的引用,而不復制對象本身。換句話說,原始對象和新對象共享同一個內(nèi)存地址。
Person person1 = new Person("Alice"); Person person2 = person1; // 引用拷貝 person2.setName("Bob"); System.out.println(person1.getName()); // 輸出 "Bob" System.out.println(person2.getName()); // 輸出 "Bob"
在上述代碼中,person1
和person2
指向同一個對象,所以改變其中一個對象的屬性,另一個對象的屬性也會被改變。
2. 淺拷貝(Shallow Copy)
淺拷貝會創(chuàng)建一個新對象,但新對象中的成員變量(如果是對象)仍然是原對象的引用。淺拷貝僅復制對象的第一層屬性。
可以通過實現(xiàn)Cloneable
接口并重寫clone
方法來實現(xiàn)淺拷貝:
class Address { String city; public Address(String city) { this.city = city; } } class Person implements Cloneable { String name; Address address; public Person(String name, Address address) { this.name = name; this.address = address; } @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); // 淺拷貝 } } public class Main { public static void main(String[] args) throws CloneNotSupportedException { Address address = new Address("New York"); Person person1 = new Person("Alice", address); Person person2 = (Person) person1.clone(); person2.name = "Bob"; person2.address.city = "Los Angeles"; System.out.println(person1.name); // 輸出 "Alice" System.out.println(person1.address.city); // 輸出 "Los Angeles" System.out.println(person2.name); // 輸出 "Bob" System.out.println(person2.address.city); // 輸出 "Los Angeles" } }
在上述代碼中,person1
和person2
擁有不同的name
屬性,但是共享同一個Address
對象。
3. 深拷貝(Deep Copy)
深拷貝不僅創(chuàng)建一個新對象,還會遞歸地復制所有成員對象。這樣,原對象和新對象完全獨立,不共享任何引用。
深拷貝可以通過手動實現(xiàn)clone
方法來完成,或者使用序列化。
手動實現(xiàn)深拷貝的示例:
class Address implements Cloneable { String city; public Address(String city) { this.city = city; } @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } } class Person implements Cloneable { String name; Address address; public Person(String name, Address address) { this.name = name; this.address = address; } @Override protected Object clone() throws CloneNotSupportedException { Person cloned = (Person) super.clone(); cloned.address = (Address) address.clone(); // 深拷貝 return cloned; } } public class Main { public static void main(String[] args) throws CloneNotSupportedException { Address address = new Address("New York"); Person person1 = new Person("Alice", address); Person person2 = (Person) person1.clone(); person2.name = "Bob"; person2.address.city = "Los Angeles"; System.out.println(person1.name); // 輸出 "Alice" System.out.println(person1.address.city); // 輸出 "New York" System.out.println(person2.name); // 輸出 "Bob" System.out.println(person2.address.city); // 輸出 "Los Angeles" } }
在上述代碼中,person1
和person2
的所有屬性都是獨立的,修改一個對象的屬性不會影響另一個對象。
通過序列化和反序列化來實現(xiàn)Java的深拷貝是一種通用且方便的方法。它可以確保對象的完整復制,包括所有嵌套的成員對象。以下是具體的實現(xiàn)步驟:
- 讓類實現(xiàn)
Serializable
接口:確保需要深拷貝的類和它包含的所有成員類都實現(xiàn)Serializable
接口。 - 使用序列化和反序列化進行深拷貝:將對象寫入字節(jié)流,然后從字節(jié)流中讀出對象,從而實現(xiàn)對象的完全復制。
下面是一個具體的例子:
示例代碼
import java.io.*; class Address implements Serializable { private static final long serialVersionUID = 1L; String city; public Address(String city) { this.city = city; } @Override public String toString() { return "Address{city='" + city + "'}"; } } class Person implements Serializable { private static final long serialVersionUID = 1L; String name; Address address; public Person(String name, Address address) { this.name = name; this.address = address; } @Override public String toString() { return "Person{name='" + name + "', address=" + address + "}"; } // 深拷貝方法 public Person deepCopy() { try { // 序列化 ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(this); oos.flush(); oos.close(); // 反序列化 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bis); return (Person) ois.readObject(); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); return null; } } } public class Main { public static void main(String[] args) { Address address = new Address("New York"); Person person1 = new Person("Alice", address); Person person2 = person1.deepCopy(); person2.name = "Bob"; person2.address.city = "Los Angeles"; System.out.println("Original: " + person1); // 原對象 System.out.println("Copy: " + person2); // 深拷貝后的對象 } }
代碼解釋
- 實現(xiàn)
Serializable
接口:Address
和Person
類都實現(xiàn)了Serializable
接口,使它們可以被序列化。 - 深拷貝方法
deepCopy
:- 序列化:使用
ObjectOutputStream
將對象寫入ByteArrayOutputStream
。 - 反序列化:使用
ObjectInputStream
從ByteArrayInputStream
中讀出對象,生成一個新的對象副本。
- 序列化:使用
- 測試深拷貝:
- 創(chuàng)建原始對象
person1
,并通過deepCopy
方法生成person2
。 - 修改
person2
的屬性,驗證原始對象person1
不受影響,證明了對象的深拷貝。
- 創(chuàng)建原始對象
這種方法的優(yōu)點是實現(xiàn)簡單,且適用于所有需要深拷貝的情況。然而,它也有一些限制,比如性能較慢(因為涉及IO操作)和必須實現(xiàn)Serializable
接口。
附:深拷貝需要注意的點
在Java中,對象的深拷貝需要注意以下幾點:
- 不可拷貝對象的狀態(tài),只能拷貝對象的狀態(tài)描述。Java中對象的狀態(tài)包括實例變量和內(nèi)部類實例等。
- 對于可變對象,深拷貝后修改原始對象中的值也會修改拷貝對象中的值。因此,在進行深拷貝時需要創(chuàng)建新的對象,而不是直接引用原始對象。
- 對于不可變對象,深拷貝后修改原始對象中的值不會影響拷貝對象中的值。
- 對于數(shù)組和集合類對象,深拷貝時需要拷貝數(shù)組或集合中的每一個元素,而不僅僅是引用。
- 對于非內(nèi)存中的對象,例如文件、網(wǎng)絡(luò)資源等,深拷貝時需要重新創(chuàng)建資源而不是簡單地引用。
- 對于非序列化的對象,深拷貝時需要通過實現(xiàn)Cloneable接口并重寫clone()方法來實現(xiàn)。
- 對于實現(xiàn)了Serializable接口的對象,可以通過序列化和反序列化來實現(xiàn)深拷貝。但是需要注意,如果對象中有不可序列化的字段,則無法通過序列化實現(xiàn)深拷貝。
總結(jié)
- 引用拷貝:只復制引用,原對象和新對象指向同一個對象。
- 淺拷貝:創(chuàng)建新對象,但不遞歸復制成員對象,成員對象仍然是共享的引用。
- 深拷貝:創(chuàng)建新對象,并遞歸復制所有成員對象,完全獨立。
這些拷貝方式在實際應(yīng)用中有不同的使用場景和適用性,根據(jù)需要選擇合適的拷貝方式可以有效管理內(nèi)存和對象關(guān)系。
到此這篇關(guān)于java中三種拷貝方法的文章就介紹到這了,更多相關(guān)java拷貝方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot整合RabbitMQ的5種模式實戰(zhàn)
本文主要介紹了SpringBoot整合RabbitMQ的5種模式實戰(zhàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08Mybatis-Plus insertBatch執(zhí)行緩慢的原因查詢
這篇文章主要介紹了Mybatis-Plus insertBatch執(zhí)行緩慢的原因查詢,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11解決springboot利用ConfigurationProperties注解配置數(shù)據(jù)源無法讀取配置信息問題
今天在學習springboot利用ConfigurationProperties注解配置數(shù)據(jù)源的使用遇到一個問題無法讀取配置信息,發(fā)現(xiàn)全部為null,糾結(jié)是哪里出了問題呢,今天一番思考,問題根源找到,下面把我的解決方案分享到腳本之家平臺,感興趣的朋友一起看看吧2021-05-05idea?maven項目啟動項目不編譯target?文件的問題及解決方法
代碼編輯器中無編譯錯誤,通過maven 的clean 、compile、package進行各種操作也都沒問題,但是單擊綠色箭頭運行(默認會先執(zhí)行IDE本身的Build操作)卻報:程序包xxx不存在,這篇文章主要介紹了解決idea maven項目啟動項目不編譯target文件問題,需要的朋友可以參考下2023-05-05SpringBoot實現(xiàn)設(shè)置動態(tài)定時任務(wù)的方法詳解
這篇文章主要介紹了SpringBoot實現(xiàn)設(shè)置動態(tài)定時任務(wù)的方法詳解,SpringBoot是一個快速開發(fā)的Java框架,而動態(tài)定時任務(wù)是指可以在運行時動態(tài)添加、修改和刪除定時任務(wù)的功能,需要的朋友可以參考下2023-10-10