21xrx.com
2025-03-14 23:39:17 Friday
文章检索 我的文章 写文章
Java方法参数传递详解及案例演示
2023-06-14 20:49:19 深夜i     11     0
方法参数 值传递 引用传递

Java中的方法参数传递是一个重要的知识点,掌握了方法参数传递的规则可以使我们更好地编写代码。本文将详细介绍Java中的方法参数传递,包括它们的类型、传递方式、引用传递等,同时提供一些实用的案例演示。让我们一起来学习吧!

1. Java方法参数传递类型

首先,让我们来了解Java中的方法参数传递类型。Java中的方法参数传递有两种类型:基本数据类型和引用数据类型。基本数据类型在传递时,是将实际值传递给被调用方法的形式参数。而引用数据类型在传递时,是将引用地址传递给被调用方法的形式参数。

以下是一个基本数据类型的案例:

public class ParamPassDemo {
  public static void main(String[] args) {
    int num1 = 10;
    int num2 = 20;
    System.out.println("传递前:num1=" + num1 + ",num2=" + num2);
    swap(num1, num2);
    System.out.println("传递后:num1=" + num1 + ",num2=" + num2);
  }
  public static void swap(int a, int b)
    int temp = a;
    a = b;
    b = temp;
  
}

输出结果:

传递前:num1=10,num2=20
传递后:num1=10,num2=20

我们发现,在swap方法中修改了a、b的值,但是在main方法中num1、num2的值却没有改变,这是因为基本数据类型传递的是值,方法调用后形式参数的值改变不会影响实际参数的值。

以下是一个引用数据类型的案例:

public class ParamPassDemo3 {
  public static void main(String[] args) {
    Student s1 = new Student("sara", 20);
    Student s2 = new Student("henry", 21);
    System.out.println("传递前:student1=" + s1.toString() + ",student2=" + s2.toString());
    swap(s1, s2);
    System.out.println("传递后:student1=" + s1.toString() + ",student2=" + s2.toString());
  }
  public static void swap(Student stu1, Student stu2)
    Student temp = stu1;
    stu1 = stu2;
    stu2 = temp;
  
}
class Student {
  String name;
  int age;
  public Student(String name, int age)
    this.name = name;
    this.age = age;
  
  @Override
  public String toString() {
    return "Student{" +
        "name='" + name + '\'' +
        ", age=" + age +
        '}';
  }
}

输出结果:

传递前:student1=Studentname='sara',student2=Studentname='henry'
传递后:student1=Studentname='sara',student2=Studentname='henry'

我们发现,在swap方法中修改了stu1、stu2的引用地址,但是在main方法中s1、s2的引用地址却没有改变,这是因为引用数据类型传递的是引用地址,方法调用后形式参数的引用地址改变不会影响实际参数的引用地址。

2. Java方法参数传递方式

Java中方法参数传递有两种方式:值传递和引用传递。值传递适用于基本数据类型,它将实际值拷贝一份给形式参数,在方法中修改形式参数的值不会影响实际参数的值。引用传递适用于引用数据类型,它将实际参数的引用地址传递给形式参数,形式参数和实际参数指向同一个对象,在方法中修改形式参数指向的对象会同时改变实际参数指向的对象。

以下是一个值传递的案例:

public class ParamPassDemo2 {
  public static void main(String[] args) {
    int num1 = 10;
    int num2 = 20;
    swap2(num1, num2);
    System.out.println(num1 + "," + num2);
  }
  public static void swap2(int x, int y)
    int temp = x;
    x = y;
    y = temp;
  
}

输出结果:

10,20

我们发现,在swap2方法中交换了x、y的值,但是在main方法中num1、num2的值却没有改变,这是因为值传递只是将实际值的一份拷贝传递给形式参数,形式参数和实际参数互相独立,互不干涉。

以下是一个引用传递的案例:

public class ParamPassDemo4 {
  public static void main(String[] args) {
    Student s1 = new Student("sara", 20);
    Student s2 = new Student("henry", 21);
    System.out.println("传递前:student1=" + s1.toString() + ",student2=" + s2.toString());
    swap2(s1, s2);
    System.out.println("传递后:student1=" + s1.toString() + ",student2=" + s2.toString());
  }
  public static void swap2(Student stu1, Student stu2)
    String temp = stu1.name;
    stu1.name = stu2.name;
    stu2.name = temp;
    int ageTemp = stu1.age;
    stu1.age = stu2.age;
    stu2.age = ageTemp;
  
}

输出结果:

传递前:student1=Student age=20,student2=Student age=21
传递后:student1=Student age=21,student2=Student age=20

我们发现,在swap2方法中交换了stu1、stu2引用地址所指向的对象的属性值,这对于main方法中的s1、s2也有影响,这是因为引用传递将实际参数的引用地址传递给形式参数,形式参数和实际参数指向同一个对象,形参、实参指向同一个对象。如果形参所对应的对象被修改了,那么实参对应的对象也会被修改。

3. Java方法参数传递关键词

方法参数、值传递、引用传递。

  
  

评论区