java 板


LINE

※ 引述《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







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:e-shopping站内搜寻

TOP