System.out.println("Before Swap, Interger1 is " + interger1);
System.out.println("Before Swap, Interger2 is " + interger2);
swap(interger1, interger2);
System.out.println("After Swap Interger1 is " + interger1);
System.out.println("After Swap Interger2 is " + interger2);
System.out.println("Before Swap i is " + i);
System.out.println("Before Swap j is " + j);
swap(i, j);
System.out.println("After Swap i is " + i);
System.out.println("After Swap j is " + j);
StringBuffer sb = new StringBuffer("I am StringBuffer");
System.out.println("Before change, sb is <" + sb + ">");
change(sb);
System.out.println("After change sb is <" + sb + ">");
}
publicstaticvoid swap(Integer ia, Integer ib) {
Integer temp = ia;
ia = ib;
ib = temp;
}
publicstaticvoid swap(int li, int lj) {
int temp = li;
li = lj;
lj = temp;
}
publicstaticvoid change(StringBuffer ia) {
ia.append(", but my content can be changed");
//ia = new StringBuffer(",but my content can be changed");
}
}
输出:
Before Swap, Interger1 is 10 Before Swap, Interger2 is 50 After Swap Interger1 is 10 After Swap Interger2 is 50 Before Swap i is 5 Before Swap j is 9 After Swap i is 5 After Swap j is 9 Before change, sb is <I am StringBuffer> After change sb is <I am StringBuffer, but my content can be changed>
cout<<"&i = "<<&i<<", It's address of i and value of iptr"<<endl;
cout<<"*iptr = "<<*iptr<<", It's value of i"<<endl;
cout<<"iptr = "<<iptr<<", It's value of iptr and address of i"<<endl;
cout<<"&iptr = "<<&iptr<<", It's address of iptr-self"<<"\n\n";
test(iptr, "pass-by-iptr");
test(&i, "pass-by-&i");
return 0;
}
void test(int* iiptr, constchar* string){
cout<<"When pass-by-value and :"<<"\n\n";
cout<<"*iiptr = "<<*iiptr<<", It's value of i"<<endl;
cout<<"iiptr = "<<iiptr<<", It's value of iiptr and address of i"<<endl;
cout<<"&iiptr = "<<&iiptr<<", It's address of iiptr-self, different with iptr!"<<"\n\n";
}
输出:
Before pass-by-value:
i = 1, It's value of i &i = 0x0012FF7C, It's address of i and value of iptr *iptr = 1, It's value of i iptr = 0x0012FF7C, It's value of iptr and address of i &iptr = 0x0012FF78, It's address of iptr-self
When pass-by-value and :
*iiptr = 1, It's value of i iiptr = 0x0012FF7C, It's value of iiptr and address of i &iiptr = 0x0012FF24, It's address of iiptr-self, different with iptr!
When pass-by-value and :
*iiptr = 1, It's value of i iiptr = 0x0012FF7C, It's value of iiptr and address of i &iiptr = 0x0012FF24, It's address of iiptr-self, different with iptr!
在C++里面的第二种就是引用传递了(pass-by-reference)。见如下示例:
cpp 代码
#include<iostream.h>
int main(){
void test(int&, constchar*);
int i = 1;
int &iref = i;
cout<<"Before pass-by-reference:"<<"\n\n";
cout<<"i = "<<i<<", It's value of i"<<endl;
cout<<"&i = "<<&i<<", It's address of i and value of iptr"<<endl;
cout<<"iref = "<<iref<<", It's value of iref and value of i"<<endl;
cout<<"&iref = "<<&iref<<", It's address of iref-self, the same as i!"<<"\n\n";
test(iref, "pass-by-iref");
test(i, "pass-by-i");
return 0;
}
void test(int &iiref, constchar* string){
cout<<"When pass-by-reference and "<<string<<"\n\n";
cout<<"iiref = "<<iiref<<", It's value of iiref and value of i"<<endl;
cout<<"&iiref = "<<&iiref<<", It's address of iiref-self, the same as i!"<<"\n\n";
}
输出:
Before pass-by-reference:
i = 1, It's value of i &i = 0x0012FF7C, It's address of i and value of iptr iref = 1, It's value of iref and value of i &iref = 0x0012FF7C, It's address of iref-self, the same as i!
When pass-by-reference and pass-by-iref
iiref = 1, It's value of iiref and value of i &iiref = 0x0012FF7C, It's address of iiref-self, the same as i!
When pass-by-reference and pass-by-i
iiref = 1, It's value of iiref and value of i &iiref = 0x0012FF7C, It's address of iiref-self, the same as i!