java学习_04.1_参数传递

博客内容仅作学习交流之用,详细内容参见网络资源,欢迎大家交流探讨!

 

对于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运行结果:
          方法得到的i的值:100
          方法(i*5)执行后i的值:500
          money的值:100

 

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的属性值:
       h:12m:45s:20
       参数pt的属性值:
       h:12m:45s:20
       对pt和time进行==比较,结果为:true
       对pt和time进行equals比较,结果为:true
       改变pt的实例变量值:
       执行方法后Time的属性值:
       h:8m:12s:24

 

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:
       time1.hour:12
       time2.hour:23
       voluefrom the function exchange:
       t1.hour:23
       t2.hour:12
       temp.hour:12
       volueafter exchange:
       time1.hour:12
       time2.hour:23
       voluefrom the function change:
       t1.hour:14
       t2.hour:20
       temp.hour:14
       volueafter exchange:
       time1.hour:14
       time2.hour:20


参考文献

[1]《Java从入门到精通》第3版_高宏静

[2]《java中引用的原理


/**
*站在巨人的肩上才能看得更远,一步一个脚印才能走得更远。分享成长,交流进步,转载请注明出处!
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值