public static void main(String[] args) { int a = 3; Com c = new Com(); c.add(a); System.out.println(a); }
运行结果 a = 8: a =3
public class Com { public void add(int a){ a += 5; System.out.println(a); } }
如果 是引用类型的话
int[] arr = new int[1]; arr[0] = 3; c.add2(arr); System.out.println(arr[0]);
输出结果是8: 8:
引用类型用的是堆内存中的同一块地址,主方法再次输出从栈里调用堆里的同一块地址,已经改变
public void add2(int arr[]){ arr[0] +=5; System.out.println(arr[0]); }