[新手上路]一D简单JAVA问题
时间:2014-04-04
来源:互联网

public class TestSwap {
public static void main(String[] argv) {
int num1=1;
int num2=2;
System.out.println("num1 is " + num1 + " and num2 is " + num2);
swap(num1, num2);
System.out.println("num1 is " + num1 + " and num2 is " + num2);
}
public static void swap(int n1, int n2) {
int temp = n1;
n1 = n2;
n2 = temp;
}
}
作者: fabregasfans 发布时间: 2014-04-04
作者: a8d7e8 发布时间: 2014-04-04
作者: me888 发布时间: 2014-04-04
唔系已经swap 咗呢??
出黎结果2句都系num1=1 同埋num2 =2

作者: fabregasfans 发布时间: 2014-04-04
不过以c++的角度看
楼主未识分call by value和call by reference的分别
作者: Susan﹏汪汪 发布时间: 2014-04-04
网上抄咗段解释
Say I want to share a web page with you.
If I tell you the URL, I'm passing by reference. You can use that URL to see the same web page I can see. If that page is changed, we both see the changes. If you delete the URL, all you're doing is destroying your reference to that page - you're not deleting the actual page itself.
If I print out the page and give you the printout, I'm passing by value. Your page is a disconnected copy of the original. You won't see any subsequent changes, and any changes that you make (e.g. scribbling on your printout) will not show up on the original page. If you destroy the printout, you have actually destroyed your copy of the object - but the original web page remains intact.

作者: me888 发布时间: 2014-04-04
作者: me888 发布时间: 2014-04-04
你应该 constants 同 variables 搞乱咗,Java syntax 有些似 Ansi C , syntax 自己改番就okay la.
所以楼主的swap功能系一大挑战
作者: Susan﹏汪汪 发布时间: 2014-04-04
Java只有call by value....没call by reference或者call by pointer
所以楼主的swap功能系一大挑战
int val;
}
public static void(Int2 a, Int2 b) ...
作者: assembly.jc 发布时间: 2014-04-04
作者: Qoo记 发布时间: 2014-04-04
唔知有咩要求,所以希望岩用~
package swap;
/*
* @author IamG33k
*/
public class Swap {
private int number_one;
private int number_two;
private int number_tmp;
public static void main(String[] args) {
Swap let_swap_it = new Swap(100,356);
int num_one = let_swap_it.GetNumberOne();
int num_two = let_swap_it.GetNumberTwo();
System.out.println("a:"+ num_one +" b:" +num_two);
let_swap_it.SwapOnetoTwo();
System.out.println("a:"+ let_swap_it.number_one +" b:" +let_swap_it.number_two);
}
public Swap(int num_one, int num_two){
number_one=num_one;
number_two=num_two;
}
public void SwapOnetoTwo(){
number_tmp=number_one;
number_one=number_two;
number_two=number_tmp;
}
public int GetNumberOne(){
int num_one=number_one;
return num_one;
}
public int GetNumberTwo(){
int num_two=number_two;
return num_two;
}
}
[ 本帖最后由 IamG33k 於 2014-3-15 03:42 PM 编辑 ]
作者: IamG33k 发布时间: 2014-04-04

anyway,唔一定要用get function,直接call都得,不过第时你再写多d program你就会发觉get function 有佢既用途~
作者: IamG33k 发布时间: 2014-04-04

有兴趣嘅 programmer 可以去睇睇

Programming Methodology (Stanford)
[ 本帖最后由 me888 於 2014-3-15 03:56 PM 编辑 ]
作者: me888 发布时间: 2014-04-04
In the method swap(), we are swapping copies of theoriginal values, instead of the original values. Since localvariables and parameter variables disappear after you exit themethod, the swapped values also disappear.
http://www.cs.umd.edu/~clin/MoreJava/Container/alist-prob-swap.html
[ 本帖最后由 kelvin_0212 於 2014-3-17 09:08 AM 编辑 ]
作者: kelvin_0212 发布时间: 2014-04-04
Java只有call by value....没call by reference或者call by pointer
所以楼主的swap功能系一大挑战
作者: HappyFirday 发布时间: 2014-04-04
第一次听java 没有call by reference
http://n2.hk/d/images/r10/mobile.jpg
Object 内部结构就用reference
作者: Susan﹏汪汪 发布时间: 2014-04-04
同javascript 一样系call by value
Object 内部结构就用reference
真心 c兄写段code黎话我知点call by reference,等我学下野!
作者: IamG33k 发布时间: 2014-04-04
但佢咁讲法,应该系有方法call by reference,
真心 c兄写段code黎话我知点call by reference,等我学下野!
因为java只有call by value、所以没办法做到
作者: Susan﹏汪汪 发布时间: 2014-04-04
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28