码迷,mamicode.com
首页 > 其他好文 > 详细

方法的参数值和值传递机制

时间:2014-11-28 01:00:52      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   ar   color   sp   java   on   数据   

1 可变个数的形参的方法:

  如: public void print(int i, String...args)

   调用: t.print(3,”hello we”,”god like”);

2 java的参数传递机制: 值传递

(1)形参是基本数据类型的:将实参的值传递给基本数据类型的变量

public static void main(String[] args) {
        TransParam1 t = new TransParam1();
        int i=33;
        int j=45;
        System.out.println("i="+i+"\tj="+j);
        t.Swap(i,j);
        System.out.println("i="+i+"\tj="+j);
        
    }
    public void Swap(int x,int y){
        int temp = x;
        x = y;
        y = temp;
    }

 

bubuko.com,布布扣

(2)形参是引用数据类型的:将实参引用变量的值(首地址)传递给形参引用类型的变量

 

public static void main(String[] args) {
        TransParam2 tp = new TransParam2();
        DataSwap ds = new DataSwap();
        System.out.println("ds.i="+ds.i+"\t ds.j="+ds.j);
        tp.Swap(ds);
        System.out.println("ds.i="+ds.i+"\t ds.j="+ds.j);
    }
    public void Swap(DataSwap d){
        int temp = d.i;
        d.i = d.j;
        d.j = temp;
    }
}
class DataSwap{
    int i=45;
    int j=66;
}

bubuko.com,布布扣

方法的参数值和值传递机制

标签:style   blog   http   ar   color   sp   java   on   数据   

原文地址:http://www.cnblogs.com/yjtm53/p/4127466.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!