作者csihcs (非天夜翔)
看板java
标题Re: [问题] 可否让阵列引数变成传值呼叫
时间Sun May 31 12:44:06 2009
※ 引述《tkcn (小安)》之铭言:
: ※ 引述《Holocaust123 (Terry)》之铭言:
: : 由此可知 java 的函数若以阵列为引数,预设是传址呼叫。
: : 有没有办法改成传值呼叫呢?
: 1. Java 没有 Poninter,只有 Reference,
: 所以根本也不会有 Call by Address。
: 2. Java 也没有 Call by Reference,
: 全部都只是 Call by Value。
: 你的程式遇到的情形,
: 其实是对 reference 变数做了 Call by Value,
: 因此传入的参数是另一份 reference 但指向同一个物件,
: 对 reference 变数来说,这是 Call by Value。
: 详情请参考本版 5054 讨论串。
: (光是本版大概就讨论了三次以上罗,不过比较完整的我只找到 5054 这串)
: 要解决你的问题,
: 请自己对阵列进行复制。
就我的认知上 C 的 call by address 是 call by value of pointer
而Java的call by reference与C++的call by reference 是不一样的
Java的call by reference与C的call by value of pointer比较像
理由如下:
C++中 call by value of pointer call by reference of pointer
指标(Pointer) (type *p) || 参照(Reference) (type &*p)
记忆体配置 配置指标记忆体(4Byte) || 不配置记忆体,本身为别名(Alias)
你可以改变所指的物件状态 你可以改变所指的物件状态
但是不能改变所指的物件 也可以改变所指的物件
class test {
public:
test(int in) {
i = in;
}
int i;
};
void func11(test *input) { void func21(test *&input) {
*input.i = 11; *input.i = 21;
} }
void func12(test *input) { void func22(test *&input) {
input = new test(12); input = new test(22);
} }
int main(int argc, char *argv[]) {
test *main_input = new test(100);
cout << *main_input.i << endl; // 100
func11(main_input);
cout << *main_input.i << endl; // 11
func21(main_input);
cout << *main_input.i << endl; // 21
func12(main_input);
cout << *main_input.i << endl; // 21 而非 12
func22(main_input);
cout << *main_input.i << endl; // 22
return 0;
}
而 java 中,method 呼叫状况与前面call by value of pointer是相同的
class JavaTest {
int i;
JavaTest(int in) {
i = in;
}
public static void method1(JavaTest input) {
input.i = 1;
}
public static void method2(JavaTest input) {
input = new JavaTest(2);
}
public static void main(String[] argv) {
JavaTest mainInput = new JavaTest(100);
System.out.println(JavaTest.i); // 100
method1(mainInput);
System.out.println(JavaTest.i); // 1
method2(mainInput);
System.out.println(JavaTest.i); // 1 而非 2
}
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 211.74.9.2
阵列的部分,你可以改变阵列中的资料,但不能改变呼叫者变数储存不同阵列,
其中阵列中的资料可视为阵列的状态。
class Test {
public static void func1(int [] arr) {
arr[0] = 10;
}
public static void func2(int [] arr) {
arr = new int[2];
}
public static void main(String [] argv) {
int [] mainArr = new int[5];
mainArr[0] = 100;
System.out.println(mainArr[0]); // 100
func1(mainArr);
System.out.println(mainArr[0]); // 10
System.out.println(mainArr.length); // 5
func2(mainArr);
System.out.println(mainArr.length); // 5
}
}
※ 编辑: csihcs 来自: 211.74.9.2 (05/31 12:51)
1F:推 tkcn:Java 没有 Call by Reference 05/31 13:19
java 有这个词但是与C++的是不同的
在昇扬 java 网站线上教学里面提到
http://java.sun.com/developer/onlineTraining/JavaIntro/contents.html#ReferenceVariableUsage
http://tinyurl.com/n6mqfo [上面网址的缩址/不放心可点上面的]
When the argument and parameter types are nonprimitive (a defined class),
this process is generally called
call by reference
because the invoked method receives a copy of a reference value.
※ 编辑: csihcs 来自: 211.74.9.2 (05/31 13:52)
2F:推 tkcn:谢谢~ 我去研究看看 05/31 14:13
3F:推 godfat:统一名词没什麽不好,很多地方by address/reference是一样的 05/31 14:24
4F:→ csihcs:并不是说统一名词不好~而是C++的Reference,Java的Reference 05/31 14:27
5F:→ csihcs:是不同的,而C++中by address/reference 又有程度上的差异 05/31 14:29
6F:推 godfat:换另一个说法,其实我找不太到 by address 的说法出处 05/31 14:51
7F:→ godfat:google 只有第一笔符合,还是问问题,下面都无关了 05/31 14:52
8F:推 PsMonkey:楼上的大大,这个拖去 PLT 讨论吧 XDXD 05/31 15:07
9F:推 godfat:啊来不及了 XDXD 05/31 15:09
10F:→ csihcs: XDXD 05/31 15:13