前些時(shí)日有人問我Java到底是值傳遞還是引用傳遞哎甲,我的第一反應(yīng)便是“肯定是引用傳遞啊”,然后思考片刻后發(fā)現(xiàn)其實(shí)Java是值傳遞扑馁。
那么如何理解Java中類似引用傳遞的行為呢蹲堂?
- 首先這要批評(píng)下Thinking In java的作者Bruce Eckel ,他在介紹這塊時(shí)的表達(dá)不清楚或详,導(dǎo)致別人想起問這個(gè)問題系羞。
- 其次,要搞清楚 引用傳遞霸琴、值傳遞椒振、指針傳遞這三者之間的區(qū)別。
舉個(gè)例子來說明Java是值傳遞梧乘。
public class Student {
int score;
public Student(int score) {
super();
this.score = score;
}
public static void Change(Student s1, Student s2) {
s1 = s2;
}
public static void Change2(Student s1) {
s1.score = 95;
}
public static void main(String[] args) {
Student s1 = new Student(59);
Student s2 = new Student(95);
Student.Change(s1, s2);
System.out.println(s1.score); //59
Student.Change2(s1);
System.out.println(s1.score); //95
}
}
如上面代碼所示澎迎,倘若Java是“引用傳遞”,那么Change()后的結(jié)果應(yīng)該是95选调,但是實(shí)際結(jié)果是59夹供,所以說明Java是值傳遞。
那么類似“引用傳遞”的行為——修改類實(shí)例的屬性該如何解釋仁堪?
在本人的理解中哮洽,Java的類≈C中的結(jié)構(gòu)體[指針],類的實(shí)例≈結(jié)構(gòu)體指針弦聂。因此這種行為應(yīng)看作指針傳遞鸟辅。對(duì)上面代碼用C改寫,就更好理解了横浑。
本人并沒有閱讀過Java實(shí)現(xiàn)源碼剔桨,類的實(shí)現(xiàn)并不是這樣,這里僅僅是為了方便理解而編寫的代碼
#include <stdio.h>
#include <stdlib.h>
typedef struct Student {
int score;
}Student;
void Change(Student *s1, Student *s2)
{
s1 = s2;
}
void Change2(Student *s1)
{
s1->score = 95;
}
int main()
{
Student *s1 = (Student*)malloc(sizeof(Student));
s1->score = 59;
Student *s2 = (Student*)malloc(sizeof(Student));
s2->score = 95;
Change(s1, s2);
printf("%d\n", s1->score); //59
Change2(s1);
printf("%d\n", s1->score); //95
return 0;
}
由于Change()沒有修改s1指針指向的空間的值徙融,所以并沒有修改到s1的值洒缀。
因此,Java引用傳遞的說法是不準(zhǔn)確的欺冀。
雖然Java中刻意回避了指針的概念树绩,但是實(shí)際上Java中實(shí)際上還是存在指針的。比如Java的數(shù)組就和C中的數(shù)組一樣隐轩,首地址是類似指針的存在饺饭。
public class ArrayTest {
public static void changeArray(int a[]) {
a[1] = 6;
}
public static void main(String[] args) {
int a[] = {1, 2, 3};
changeArray(a);
for (int i : a) {
System.out.print(i + " ");
}
// output: 1 6 3
}
}
對(duì)于Bruce Eckel的說法,理解成變量是引用型的更為準(zhǔn)確职车。如int a = 1從底層的角度看瘫俊,也可以理解是變量a引用了一個(gè)內(nèi)容為1的地址鹊杖。也就是引用無(wú)處不在(最后一段看不懂就算了,逃