代码示例:
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,包装类等对象不可变性
Java参数传递机制:基本类型与引用类型的区别
该代码示例展示了Java中基本类型与引用类型在方法参数传递时的区别。对于基本类型,传递的是值的副本,而引用类型传递的是对象的引用。文章强调了String、包装类及对象的不可变性特点。

被折叠的 条评论
为什么被折叠?



