Java對象傳遞與返回的細節(jié)問題詳析
1.傳遞引用
在一個方法中將一個對象的引用傳遞給另外一個方法,引用指向的對象是同一個
public class Person { int age; String name; public Person(int age, String name) { this.age = age; this.name = name; } public static void main(String[] args) { Person p=new Person(18, "tom"); System.out.println("main: "+p); f(p); } public static void f(Person p) { System.out.println("f(): "+p); } }
引用別名
public static void main(String[] args) { Person p=new Person(18, "tom"); Person p2=p; p2.age++; System.out.println(p.age);//19 }
引用p和p2指向的是同一個對象,p2對對象的屬性進行操作,當使用引用p訪問對象的屬性時當然也改變,同樣的情況也發(fā)生在對象引用在方法之間的傳遞,如下面的代碼:
public static void main(String[] args) { Person p=new Person(18, "tom"); f(p); System.out.println(p.age);//19 } public static void f(Person p) { p.age++; }
2. 創(chuàng)建本地副本
幾個概念:
- 引用別名會在方法參數(shù)是對象類型時自動發(fā)生
- 沒有本地對象,只有本地引用(方法中創(chuàng)建的對象存在于堆中,只有引用變量存在于方法棧中)
- 引用是有作用域的,而對象沒有
- Java中的對象的生命周期并不是一個問題(垃圾回收機制)
2.1 值傳遞
Java中方法之間只有值傳遞
傳遞基本類型時,傳遞的是基本類型的值的拷貝
public static void main(String[] args) { int a=100; change(a); System.out.println(a);//100 不受影響 } public static void change(int a) { a=99; }
傳遞對象時,傳遞的是對象的引用拷貝
public static void main(String[] args) { Person p=new Person(18, "tom"); f(p); System.out.println(p.age);//還是18 f()中p指向了一個新的對象 不影響main函數(shù) } public static void f(Person p) { p=new Person(20, "bob"); }
傳遞對象時如果在另外一個方法中對對象的屬性進行操作會對main方法產(chǎn)生“副作用”, 但是如果只是簡單的對引用進行操作是沒有影響的
2.2 對象克隆
步驟:
類實現(xiàn)Cloneable空接口,默認情況下不希望所有的類都有克隆能力,當需要某個類有克隆能力時就需要實現(xiàn)該接口作為一種“可克隆”的標記,否則克隆時會報錯CloneNotSupportedException
public interface Cloneable { }
重寫clone方法,clone方法是Object類中的,它在Object類中的是一個protected的本地方法,需要重寫,加上public修飾符,否則只能在當前類中使用clone方法
protected native Object clone() throws CloneNotSupportedException;
public class Person implements Cloneable { int age; String name; public Person(int age, String name) { this.age = age; this.name = name; } public Person clone() throws CloneNotSupportedException { return (Person) super.clone(); } public static void main(String[] args) throws CloneNotSupportedException { Person p1=new Person(18, "tom"); Person p2=p1.clone(); System.out.println(p1+" name: "+p1.name+" age: "+p1.age); System.out.println(p2+" name: "+p2.name+" age: "+p2.age); } }
重寫clone方法時實際是就是調(diào)用Object類中的本地clone方法,Object類中的clone方法做了哪些工作?
Object類中clone方法負責創(chuàng)建正確大小的存儲空間,并執(zhí)行了從原始對象中所有二進制位到新對象內(nèi)存中的按位復制。
2.3 淺拷貝問題
場景:Person類中增加一個引用類型的屬性Country, 表示這個人所屬的國家,然后進行克隆
package test; class Country{ String nation; public Country(String nation) { super(); this.nation = nation; } } public class Person implements Cloneable { int age; String name; Country country; public Person(int age, String name,Country country) { this.age = age; this.name = name; this.country=country; } public Person clone() throws CloneNotSupportedException { return (Person) super.clone(); } public static void main(String[] args) throws CloneNotSupportedException { Country country=new Country("China"); Person p1=new Person(18, "tom",country); Person p2=p1.clone(); System.out.println(p1+" name: "+p1.name+" age: "+p1.age+" country: "+p1.country); System.out.println(p2+" name: "+p2.name+" age: "+p2.age+" country: "+p2.country); p1.name="bob"; p1.country.nation="America"; System.out.println(p1+" name: "+p1.name+" age: "+p1.age+" country: "+p1.country.nation); System.out.println(p2+" name: "+p2.name+" age: "+p2.age+" country: "+p2.country.nation); } }
問題描述: 當Person類中有一個引用類型屬性時,對于基本類型數(shù)據(jù)和String類型是對值進行拷貝的,因此改變p1的name不影響p2的name, 但是對于Country這種引用類型,在clone時僅僅是拷貝了一份對象的引用,拷貝的引用和原來的引用指向的是同一個對象,因此p1改變country的屬性時,p2的country的屬性也發(fā)生了改變
2.4 深拷貝
解決淺拷貝問題的關(guān)鍵點在于不僅僅是拷貝引用,而且要拷貝一份引用指向的對象,深拷貝有兩種方式:
- 逐個對引用指向的對象進行淺拷貝
- 使用序列化方式進行深拷貝
2.4.1 引用類型逐個淺拷貝
如果一個類A中有多個引用類型,那么這些引用類型的類需要實現(xiàn) Cloneable接口,在對A的對象進行克隆時,逐個淺拷貝其中的引用類型
eg: Person類中有Country引用類型,在進行clone時,單獨對country進行淺拷貝
package test; class Country implements Cloneable{ String nation; public Country(String nation) { super(); this.nation = nation; } public Country clone() throws CloneNotSupportedException { return (Country) super.clone(); } } public class Person implements Cloneable { int age; String name; Country country; public Person(int age, String name,Country country) { this.age = age; this.name = name; this.country=country; } public Person clone() throws CloneNotSupportedException { Person p=null; p=(Person) super.clone(); p.country=p.country.clone();//對country進行淺拷貝 return p; } public static void main(String[] args) throws CloneNotSupportedException { Country country=new Country("China"); Person p1=new Person(18, "tom",country); Person p2=p1.clone(); System.out.println(p1+" name: "+p1.name+" age: "+p1.age+" country: "+p1.country); System.out.println(p2+" name: "+p2.name+" age: "+p2.age+" country: "+p2.country); p1.name="bob"; p1.country.nation="America"; System.out.println(p1+" name: "+p1.name+" age: "+p1.age+" country: "+p1.country.nation); System.out.println(p2+" name: "+p2.name+" age: "+p2.age+" country: "+p2.country.nation); } }
2.4.2 序列化方式進行深拷貝
package test; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; class Country implements Serializable{ private static final long serialVersionUID = 1L; String nation; public Country(String nation) { super(); this.nation = nation; } } public class Person implements Cloneable,Serializable { private static final long serialVersionUID = 1L; int age; String name; Country country; public Person(int age, String name,Country country) { this.age = age; this.name = name; this.country=country; } public Person clone() throws CloneNotSupportedException { Person p=null; ObjectInputStream ois=null; ObjectOutputStream oos=null; ByteArrayInputStream bais=null; ByteArrayOutputStream baos=null; try { baos=new ByteArrayOutputStream(); oos=new ObjectOutputStream(baos); oos.writeObject(this);//Person對象序列化 序列化寫入到baos流中 bais=new ByteArrayInputStream(baos.toByteArray()); ois=new ObjectInputStream(bais);//從baos流中讀取數(shù)據(jù)到ois p=(Person) ois.readObject();//ois讀取對象數(shù)據(jù) } catch (Exception e) { e.printStackTrace(); }finally { if(bais!=null) try { bais.close(); } catch (IOException e) { e.printStackTrace(); } if(baos!=null) try { baos.close(); } catch (IOException e) { e.printStackTrace(); } if(oos!=null) try { oos.close(); } catch (IOException e) { e.printStackTrace(); } if(ois!=null) try { ois.close(); } catch (IOException e) { e.printStackTrace(); } } return p; } public static void main(String[] args) throws CloneNotSupportedException { Country country=new Country("China"); Person p1=new Person(18, "tom",country); Person p2=p1.clone(); System.out.println(p1+" name: "+p1.name+" age: "+p1.age+" country: "+p1.country); System.out.println(p2+" name: "+p2.name+" age: "+p2.age+" country: "+p2.country); p1.name="bob"; p1.country.nation="America"; System.out.println(p1+" name: "+p1.name+" age: "+p1.age+" country: "+p1.country.nation); System.out.println(p2+" name: "+p2.name+" age: "+p2.age+" country: "+p2.country.nation); } }
總結(jié): 實現(xiàn)一個可克隆的類的步驟
- 實現(xiàn)Cloneable接口
- 重寫clone方法
- 在重寫的clone方法中調(diào)用super.clone()方法
- 在重寫的clone方法中捕獲異常
總結(jié)
到此這篇關(guān)于Java對象傳遞與返回細節(jié)問題的文章就介紹到這了,更多相關(guān)Java對象傳遞與返回內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Java代碼將IP地址轉(zhuǎn)換為int類型的方法
這篇文章主要介紹了使用Java代碼將IP地址轉(zhuǎn)換為int類型的方法,這也是各大計算機考試和ACM以及面試的常見基礎問題,需要的朋友可以參考下2015-08-08