博客内容仅作学习交流之用,详细内容参见网络资源,欢迎大家交流探讨!
对于java参数的传递,之前分享了一些文章,有比较基础的、也有比较晦涩难懂的,由于刚开始学习java不久,对于内容的准确程度,还需要大家共同学习探讨。这里再将《Java从入门到精通》中相关内容(5.4小节)摘出来,以做总结。
在各种程序设计语言中,参数传递一般有两种,①“值传”,②“传地址”。“值传”是指在调用方法时把参数的值直接传递给方法,而“传地址”则是给方法提供参数的地址。java的参数传递都为值调用,当涉及到对象的传递时,它又不是简单的值传。
1.基本类型的参数传递
java中使用的都是传值引用,所以参数得到的都是参数值得拷贝。
//Author:BonJean
//java参数传递,基本类型的值传递
//date:2014.07.23
//package package_01;
public class Demo_00 {
public static void main(String[] args) {
ParamTransferpt=new ParamTransfer();
pt.money=100;
pt.amethod(pt.money);
}
}
class ParamTransfer{
publicint money;
void amethod(int i){
System.out.println("方法得到的i的值:"+i);
i*=5;
System.out.println("方法(i*5)执行后i的值:"+i);
System.out.println("money的值:"+money);
}
}
Demo_00运行结果:
2.对象类型的参数传递
对于简单类型的参数传递,不过是传递一个原来的值得拷贝给参数,但是对于对象型的参数就比较复杂了。java采取一种特殊的方法,是把对象的引用(关于引用的概念,可以参考另一篇博文《java中引用的原理》)拷贝一份传递给参数。虽然也是传值,但是传递的并不是对象中变量的具体数值,而是该对象引用中的值。
程序演示,首先定义一个Time类,
class Time{
publicint hour;
publicint minute;
publicint second;
}
以下演示程序中都用到了这个类。
//Author:yekong
//java函数参数传递
//2014.07.23
// packagepackage_01;
public class Demo_01 {
Time t;
public static void main(String[] args){
Demo_01de_01=new Demo_01();
de_01.t=newTime();
de_01.t.hour=12;
de_01.t.minute=45;
de_01.t.second=20;
System.out.println("Time的属性值:");
System.out.println("h:"+de_01.t.hour+"m:"+de_01.t.minute+"s:"+de_01.t.second);
//将对象作为参数传递给方法
de_01.method(de_01.t);
System.out.println("执行方法后Time的属性值:");
System.out.println("h:"+de_01.t.hour+"m:"+de_01.t.minute+"s:"+de_01.t.second);
}
voidmethod(Time pt){
System.out.println("参数pt的属性值:");
System.out.println("h:"+pt.hour+"m:"+pt.minute+"s:"+pt.second);
System.out.println("对pt和time进行==比较,结果为:"+(pt==this.t));
System.out.println("对pt和time进行equals比较,结果为:"+(pt.equals(this.t)));
System.out.println("改变pt的实例变量值:");
pt.hour=8;
pt.minute=12;
pt.second=24;
}
}
Demo_01程序运行结果:
Time的属性值:
Demo_02和Demo_01大致相同,此处不再赘述。
//Author:yekong
//java函数参数传递
//2014.07.23
// package package_01;
public class Demo_03 {
public static void main(String[] args){
//create two instance
Timetime1=new Time();
Timetime2=new Time();
//setinstance's value
time1.hour=12;
time2.hour=23;
System.out.println("voluebefore exchange:");
System.out.println("time1.hour:"+time1.hour);
System.out.println("time2.hour:"+time2.hour);
//usefunction
Demo_03.swap(time1,time2);
System.out.println("volueafter exchange:");
System.out.println("time1.hour:"+time1.hour);
System.out.println("time2.hour:"+time2.hour);
Demo_03.change(time1,time2);
System.out.println("volueafter exchange:");
System.out.println("time1.hour:"+time1.hour);
System.out.println("time2.hour:"+time2.hour);
}
staticvoid swap(Time t1,Time t2){
Timetemp;
temp=t1;
t1=t2;
t2=temp;
System.out.println("voluefrom the function exchange:");
System.out.println("t1.hour:"+t1.hour);
System.out.println("t2.hour:"+t2.hour);
System.out.println("temp.hour:"+temp.hour);
}
staticvoid change(Time t1,Time t2){
Timetemp;
temp=t1;
// t1=t2;
// t2=temp;
t1.hour=14;
t2.hour=20;
System.out.println("voluefrom the function change:");
System.out.println("t1.hour:"+t1.hour);
System.out.println("t2.hour:"+t2.hour);
System.out.println("temp.hour:"+temp.hour);
}
}
Demo_03程序运行结果:
volue before exchange:
参考文献
[1]《Java从入门到精通》第3版_高宏静
[2]《java中引用的原理》
/**
*站在巨人的肩上才能看得更远,一步一个脚印才能走得更远。分享成长,交流进步,转载请注明出处!
*/