1.基本类型:形式参数的改变不影响实际参数。
2.引用类型:形式参数的改变直接影响实际参数。
1 //形式参数是基本类型
2 class Demo {
3 public int sum(int a,int b) {
4 return a + b;
5 }
6 }
7
8 //形式参数是引用类型
9 class Student {
10 public void show() {
11 System.out.println("我爱学习");
12 }
13 }
14
15 class StudentDemo {
16 //如果你看到了一个方法的形式参数是一个类类型(引用类型),这里其实需要的是该类的对象。
17 public void method(Student s) { //调用的时候,把main方法中的s的地址传递到了这里 Student s = new Student();
18 s.show();
19 }
20 }
21
22 class ArgsTest {
23 public static void main(String[] args) {
24 //形式参数是基本类型的调用
25 Demo d = new Demo();
26 int result = d.sum(10,20);
27 System.out.println("result:"+result);
28 System.out.println("--------------");
29
30 //形式参数是引用类型的调用
31 //需求:我要调用StudentDemo类中的method()方法
32 StudentDemo sd = new StudentDemo();
33 //创建学生对象
34 Student s = new Student();
35 sd.method(s); //把s的地址给到了这里
36 }
37 }
如果你看到一个方法需要的参数是一个类名,就应该知道这里实际需要的是一个具体的对象。