06 方法参数的值传递机制

06 方法参数的值传递机制

一、方法,必须由其所在类或对象调用采用意义,若方法含有参数:

  • 形参:方法定义时,声明的参数;
  • 实参:方法调用时,实际传给形参的数据;

二、关于变量的赋值

如果变量是基本数据类型,此时赋值的是变量所保存的数据值;

如果变量是引用数据类型,此时复用的变量所保存的数据的地址值;

public class ValueTransferTest {
    public static void main(String[] args) {
        int m = 10;
        int n = m;
        System.out.println("m = " + m + ", n = " + n);
        n = 20;
        System.out.println("m = " + m + ", n = " + n);

        Order o1 = new Order();
        o1.orderId = 1001;
        Order o2 = o1;//赋值以后,o1和o2的地址值相同,都指向了堆空间中统一个对象实体
        System.out.println("o1.orderId = " + o1.orderId + ", o2.orderId = " + o2.orderId);
        o2.orderId = 1002;
        System.out.println("o1.orderId = " + o1.orderId + ", o2.orderId = " + o2.orderId);
    }
}

class  Order {
    int orderId;
}

三、Java的实参值如何传入方法呢?

Java里方法的参数传递方式只有一种:值传递,即将实际参数值的副本(赋值品)传入方法中,而参数本身不受影响。

  • 形参是基本数据类型,将实参基本数据类型变量的“数据值”传递给形参;
  • 形参是引用数据类型,将实参引用数据类型变量的“地址值”传递给形参;

四、方法形参的传递机制

1、针对基本数据类型

如果参数是基本数据类型,此时实参赋给形参的是实参真实存储的数据值。

public class ValueTransferTest {
    public static void main(String[] args) {
        //交换两个变量的值的操作
        int m = 10;
        int n = 20;
        System.out.println("m = " + m + ", n =" + n);
        
        ValueTransferTest test = new ValueTransferTest();
        test.swap(m,n);
        System.out.println("m = " + m + ", n =" + n);
    }

    public void swap(int m, int n) {
        int temp = m;
        m = n;
        n = temp;
    }
}

2、针对引用数据类型

如果参数是引用数据类型,此时实参赋给形参的是实参存储数据的地址值。

public class ValueTransferTest {
    public static void main(String[] args) {
        Data data = new Data();
        data.m = 10;
        data.n = 20;
        System.out.println("m = " + data.m + ", n =" + data.n);
        //交换m和n的值
        ValueTransferTest test = new ValueTransferTest();
        test.swap(data);
        System.out.println("m = " + data.m + ", n =" + data.n);
    }

    public void swap(Data data) {
        int temp= data.m;
        data.m = data.n;
        data.n = temp;
    }
}

class  Data {
    int m;
    int n;
}

五、练习

1、阅读代码并进行内存解析

package com.william.java1;

public class ValueTransferTest {
    public static void main(String[] args) {
        ValueTransferTest test = new ValueTransferTest();
        test.first();
    }

    public void first() {
        int  i = 5;
        Value v = new Value();
        v.i = 25;
        second(v, i);
        System.out.println(v.i + " " + i);
    }

    public void second(Value v, int i) {
        i = 0;
        v.i = 20;
        Value val = new Value();
        System.out.println(v.i + " " + i);
    }
}

class  Value {
    int i = 15;
}

2、补充代码

public class ValueTransferTest {
    public static void main(String[] args) {
        int a = 10;
        int b = 10;
        method(a,b);//需要在method方法被调用之后,仅打印出a=100,b=200.请写出method方法的代码
        System.out.println("a = " + a);
        System.out.println("b = " + b);
    }
    //代码编写处
    public static void method(int i, int j) {
        i = i * 10;
        j = j * 20;
        System.out.println("a = " + i);
        System.out.println("b = " + j);
        System.exit(0);
    }
}

3、定义个int型的数据:int[] arr = new int[]{12,3,3,34,56,77,432};让数组的每个位置上的值去除以首位置的元素,得到的结果,作为该位置上的新值,遍历新的数组。

public class ValueTransferTest {
    public static void main(String[] args) {
        //方法一
        int[] arr1 = new int[]{12,3,3,34,56,77,432};
        int temp = arr1[0];
        for(int i = 0; i < arr1.length; i++) {
            arr1[i] = arr1[i] / temp;
        }
        for(int i = 0; i < arr1.length; i++) {
            System.out.print(arr1[i] + ";");
        }
        System.out.println();
        //方法二
        int[] arr2 = new int[]{12,3,3,34,56,77,432};
        for(int i = arr2.length - 1; i >= 0; i--) {
            arr2[i] = arr2[i] / arr2[0];
        }
        for(int i = 0; i < arr2.length; i++) {
            System.out.print(arr2[i] + ";");
        }
        System.out.println();
    }
}

4、数组对象输出结果

public class ValueTransferTest {
    public static void main(String[] args) {
        int[] arr1 = new int[]{1,2,3};
        System.out.println(arr1);//地址值
        
        char[] arr2 = new char[]{'a','b','c'};
        System.out.println(arr2);//abc
    }
}

5、将对象所谓参数传递给方法

(1)定义一个Circle类,包含一个double型的radius属性代表圆的半径,一个findArea()方法返回圆的面积;

(2)定义一个类PassObject,在类中定义一个方法printAreas(),该方法的定义如下:public void printAreas(Circle c, int time);在printAreas方法中打印输出1到time之间的每一个整数半径值,以及对应的面积。

例如,time为5,则输出1、2、3、4、5,以及对应的圆面积。

(3)在main方法中调用printAreas()方法,调用完毕后数据当前半径值。

public class Circle {
    double radius;

     public double findArea() {
         return Math.PI * radius * radius;
     }
}
public class PassObject {
    public static  void  main(String[] args) {
        PassObject test = new PassObject();
        Circle c = new Circle();
        test.printAreas(c, 5);
        System.out.println("now radius is " + c.radius);
    }

    public void printAreas(Circle c, int time) {
        System.out.println("Radius\t\tArea");
        for (int i = 1 ;i <= time; i ++) {
            c.radius = i;
            System.out.println(c.radius	+ "\t\t" + c.findArea());
        }
        c.radius = time + 1;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值