代码示例:
public class Demo {
public static void main(String[] args) {
int i = 1;
String str = "hello";
Integer num = 200;
int[] arr = {1,2,3,4,5};
MyData my = new MyData();
change(i, str, num, arr, my);
System.out.println(i);
System.out.println(str);
System.out.println(num);
System.out.println(Arrays.toString(arr));
System.out.println(my.a);
}
public static void change(int j, String s, Integer n, int[] a, MyData m) {
j += 1;
s += "hello";
n += 1;
a[0] = 2;
m.a += 1;
}
}
class MyData {
int a = 10;
}
执行结果:
1
hello
200
[2, 2, 3, 4, 5]
11
方法的参数传递机制
- 形参是本数据类型
- 传递数据值
- 实参是引用数据类型
- 传递地址值
- 特殊的类型:string,包装类等对象不可变性