java傳遞是引用的拷貝,既不是引用本身,更不是對象

來源:互聯網
上載者:User
Code
public class Testit {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public static void main(String[] args) {
Testit a = new Testit();
a.setName("a");
Testit b = new Testit();
b.setName("b");
System.out
.println("before swap: " + "a=" + a + " name: " + a.getName());
swap(a, b);
System.out
.println("before swap: " + "a=" + a + " name: " + a.getName());
}

private static void swap(Testit swap1, Testit swap2) {

Testit temp;
temp = swap1;
swap1 = swap2;
swap2 = temp;
swap1.setName("a's name");
swap2.setName("b's name");
}

}

1、首先弄清楚一個問題:Java有沒有指標?

對於在C和C++裡頭曾經給我們帶來歡
樂同時也有無限痛苦的指標,很多人寧願它再也不要出現在Java裡頭。然而事實上,Java是有指標的,Java中每個對象(除基礎資料型別 (Elementary Data Type)以外)的標識
符都屬於指標的一種。但它們的使用受到了嚴格的限制和防範,在<Thinking in Java>一書中稱它們為控制代碼。

2、傳遞控制代碼
將控制代碼傳遞進入一個方法時,指向的仍然是相同的對象。
public class Testit {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public static void main(String[] args) {
Testit a = new Testit();
a.setName("a");
Testit b = new Testit();
b.setName("b");
System.out.println("before swap: " + "a=" + a + " name: " + a.getName());
swap(a,b);
}

private static void swap(Testit swap1, Testit swap2) {
System.out.println("swaping: " + "a= " + swap1 + " name: " + swap1.getName());
Testit temp;
temp = swap1;
swap1 = swap2;
swap2 = temp;
}

}

輸出結果:
before swap: a=com.lib.Testit@16930e2 name: a
swaping: a= com.lib.Testit@16930e2 name: a

3、一個控制代碼的傳遞會使調用者的對象發生意外的改變。
public class Testit {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public static void main(String[] args) {
Testit a = new Testit();
a.setName("a");
Testit b = new Testit();
b.setName("b");
System.out.println("before swap: " + "a=" + a + " name: " + a.getName());
swap(a,b);
System.out.println("after swap: " + "a=" + a + " name: " + a.getName());
}

private static void swap(Testit swap1, Testit swap2) {
Testit temp;
temp = swap1;
swap1 = swap2;
swap2 = temp;
swap1.setName("a's name");
swap2.setName("b's name");
}

}

輸出結果:
before swap: a=com.lib.Testit@16930e2 name: a
after swap: a=com.lib.Testit@16930e2 name: b's name

我們看到,a依舊是原來那個a,但name卻不是原來那個name了!
在swap()方法中,swap1和swap2互相換過來了,這個時候swap2指向的是a,所以在setName()的時候改變的是a的name,而不是b的name。

為什麼會這樣呢?
liang_chen兄高見:Java裡的傳值實際上是拷貝引用,而不是拷貝對象。

總結:
1:對於實值型別的參數來說,傳遞的是值的拷貝.
2:對於參考型別的參數來說,傳遞的是引用本身的拷貝.
所以關鍵要看你如何理解傳值中的這個“值”了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.