Java對(duì)象傳遞與返回的細(xì)節(jié)問題詳析
1.傳遞引用
在一個(gè)方法中將一個(gè)對(duì)象的引用傳遞給另外一個(gè)方法,引用指向的對(duì)象是同一個(gè)
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指向的是同一個(gè)對(duì)象,p2對(duì)對(duì)象的屬性進(jìn)行操作,當(dāng)使用引用p訪問對(duì)象的屬性時(shí)當(dāng)然也改變,同樣的情況也發(fā)生在對(duì)象引用在方法之間的傳遞,如下面的代碼:
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)建本地副本
幾個(gè)概念:
- 引用別名會(huì)在方法參數(shù)是對(duì)象類型時(shí)自動(dòng)發(fā)生
- 沒有本地對(duì)象,只有本地引用(方法中創(chuàng)建的對(duì)象存在于堆中,只有引用變量存在于方法棧中)
- 引用是有作用域的,而對(duì)象沒有
- Java中的對(duì)象的生命周期并不是一個(gè)問題(垃圾回收機(jī)制)
2.1 值傳遞
Java中方法之間只有值傳遞
傳遞基本類型時(shí),傳遞的是基本類型的值的拷貝
public static void main(String[] args) {
int a=100;
change(a);
System.out.println(a);//100 不受影響
}
public static void change(int a) {
a=99;
}
傳遞對(duì)象時(shí),傳遞的是對(duì)象的引用拷貝
public static void main(String[] args) {
Person p=new Person(18, "tom");
f(p);
System.out.println(p.age);//還是18 f()中p指向了一個(gè)新的對(duì)象 不影響main函數(shù)
}
public static void f(Person p) {
p=new Person(20, "bob");
}
傳遞對(duì)象時(shí)如果在另外一個(gè)方法中對(duì)對(duì)象的屬性進(jìn)行操作會(huì)對(duì)main方法產(chǎn)生“副作用”, 但是如果只是簡單的對(duì)引用進(jìn)行操作是沒有影響的
2.2 對(duì)象克隆
步驟:
類實(shí)現(xiàn)Cloneable空接口,默認(rèn)情況下不希望所有的類都有克隆能力,當(dāng)需要某個(gè)類有克隆能力時(shí)就需要實(shí)現(xiàn)該接口作為一種“可克隆”的標(biāo)記,否則克隆時(shí)會(huì)報(bào)錯(cuò)CloneNotSupportedException
public interface Cloneable {
}
重寫clone方法,clone方法是Object類中的,它在Object類中的是一個(gè)protected的本地方法,需要重寫,加上public修飾符,否則只能在當(dāng)前類中使用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方法時(shí)實(shí)際是就是調(diào)用Object類中的本地clone方法,Object類中的clone方法做了哪些工作?
Object類中clone方法負(fù)責(zé)創(chuàng)建正確大小的存儲(chǔ)空間,并執(zhí)行了從原始對(duì)象中所有二進(jìn)制位到新對(duì)象內(nèi)存中的按位復(fù)制。
2.3 淺拷貝問題
場景:Person類中增加一個(gè)引用類型的屬性Country, 表示這個(gè)人所屬的國家,然后進(jìn)行克隆
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);
}
}
問題描述: 當(dāng)Person類中有一個(gè)引用類型屬性時(shí),對(duì)于基本類型數(shù)據(jù)和String類型是對(duì)值進(jìn)行拷貝的,因此改變p1的name不影響p2的name, 但是對(duì)于Country這種引用類型,在clone時(shí)僅僅是拷貝了一份對(duì)象的引用,拷貝的引用和原來的引用指向的是同一個(gè)對(duì)象,因此p1改變country的屬性時(shí),p2的country的屬性也發(fā)生了改變
2.4 深拷貝
解決淺拷貝問題的關(guān)鍵點(diǎn)在于不僅僅是拷貝引用,而且要拷貝一份引用指向的對(duì)象,深拷貝有兩種方式:
- 逐個(gè)對(duì)引用指向的對(duì)象進(jìn)行淺拷貝
- 使用序列化方式進(jìn)行深拷貝
2.4.1 引用類型逐個(gè)淺拷貝
如果一個(gè)類A中有多個(gè)引用類型,那么這些引用類型的類需要實(shí)現(xiàn) Cloneable接口,在對(duì)A的對(duì)象進(jìn)行克隆時(shí),逐個(gè)淺拷貝其中的引用類型
eg: Person類中有Country引用類型,在進(jìn)行clone時(shí),單獨(dú)對(duì)country進(jìn)行淺拷貝
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();//對(duì)country進(jìn)行淺拷貝
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 序列化方式進(jìn)行深拷貝
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對(duì)象序列化 序列化寫入到baos流中
bais=new ByteArrayInputStream(baos.toByteArray());
ois=new ObjectInputStream(bais);//從baos流中讀取數(shù)據(jù)到ois
p=(Person) ois.readObject();//ois讀取對(duì)象數(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é): 實(shí)現(xiàn)一個(gè)可克隆的類的步驟
- 實(shí)現(xiàn)Cloneable接口
- 重寫clone方法
- 在重寫的clone方法中調(diào)用super.clone()方法
- 在重寫的clone方法中捕獲異常
總結(jié)
到此這篇關(guān)于Java對(duì)象傳遞與返回細(xì)節(jié)問題的文章就介紹到這了,更多相關(guān)Java對(duì)象傳遞與返回內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java 基礎(chǔ)之JavaBean屬性命名規(guī)范問題
這篇文章主要介紹了java 基礎(chǔ)之JavaBean屬性命名規(guī)范問題的相關(guān)資料,需要的朋友可以參考下2017-05-05
使用Java代碼將IP地址轉(zhuǎn)換為int類型的方法
這篇文章主要介紹了使用Java代碼將IP地址轉(zhuǎn)換為int類型的方法,這也是各大計(jì)算機(jī)考試和ACM以及面試的常見基礎(chǔ)問題,需要的朋友可以參考下2015-08-08
idea 右鍵項(xiàng)目沒有run 運(yùn)行選項(xiàng)
這篇文章主要介紹了idea 右鍵項(xiàng)目沒有run 運(yùn)行選項(xiàng),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06

