传递的是值的拷贝,对原来的数据无影响
public class TempTest { //值传递 private void test1(int a){ a=5; System.out.println("test1: "+a); } public static void main(String[] args) { TempTest tempTest = new TempTest(); int a = 3; //传递后,test1方法对变量的改变不影响这里的a tempTest.test1(a); System.out.println("main: "+a); } }传递的是内存空间的引用,实质上指向的是同一块内存空间
public class TempTest { //引用传递 private void test1(A a){ a.age = 20; System.out.println("test1: "+a.age); } public static void main(String[] args) { TempTest tempTest = new TempTest(); A a = new A(); a.age = 10; //引用传递传递的是引用改变同一块内存空间 tempTest.test1(a); System.out.println("main: "+a.age); } }