值传递和引用传递的区别
看案例:
package com.cxy.juc; public class Person { private String name; public Person(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
package com.cxy.juc; public class TrabdDemo { public void changeValue(int age){ age=30; } public void changeValue2(Person person){ person.setName("1111111111"); } public void changeValue3(String str){ str ="xxxxxxxxxxxxx"; } public static void main(String[] args) { TrabdDemo trabdDemo =new TrabdDemo(); int age =20; trabdDemo.changeValue(age); System.out.println(age); Person ps =new Person("aaaa"); trabdDemo.changeValue2(ps); System.out.println(ps.getName()); String str ="abc"; trabdDemo.changeValue3(str); System.out.println(str); } }
结果:
com.cxy.juc.TrabdDemo 20 1111111111 abc Process finished with exit code 0
基础数据传输的复印件,即使在中间过程中修改了,那么值也不会变,
笔记转移,由于在有道云的笔记转移,写的时间可能有点久,如果有错误的地方,请指正