仅作为个人笔记、记录,可能会有错误,参考请慎重,欢迎指正讨论!
基本类型的包装类型的对象:
public class Test {
public static void main(String[] args) {
String string = "冰墩墩";
System.out.println("==========String改变属性值===========");
System.out.println("原对象:" + string);
System.out.println("方法返回值:" + changeString(string));
System.out.println("现对象:" + string);
Integer integer = 2008;
System.out.println("==========Integer改变属性值===========");
System.out.println("原对象:" + integer);
System.out.println("方法返回值:" +changeInteger(integer));
System.out.println("现对象:" + integer);
}
public static String changeString(String object) {
object += "雪容融";
return object;
}
public static Integer changeInteger(Integer object) {
object += 14;
return object;
}
}
输出:
==========String改变属性值===========
原对象:冰墩墩
方法返回值:冰墩墩雪容融
现对象:冰墩墩
==========Integer改变属性值===========
原对象:2008
方法返回值:2022
现对象:2008
上面以String和Integer为例,当我们在使用另一个方法对某一个包装类型进行修改的时候,修改的其实是它们的副本,也就是复制了一份,然后修改了复制后的,原包装类型还是没有改变。
自定义类型的对象:
首先定义一个Student类型:
import lombok.Data;
@Data
public class StudentVo {
private String studentName;
private Integer studentId;
private Integer age;
public StudentVo(String studentName, Integer studentId, Integer age) {
setStudentId(studentId);
setStudentName(studentName);
setAge(age);
}
}
然后进行测试:
public class TestApplication {
public static void main(String[] args) {
StudentVo studentVo = new StudentVo("冰墩墩",1,1);
System.out.println("==========对象改变属性值===========");
System.out.println("原对象:" + studentVo);
System.out.println("方法返回值:" + changeObject(studentVo));
System.out.println("现对象:" + studentVo);
List<StudentVo> studentVoList = new ArrayList<>();
studentVoList.add(studentVo);
System.out.println("==========ArrayList类型改变===========");
System.out.println("原对象:" + studentVoList);
System.out.println("方法返回值:" + changeList(studentVoList));
System.out.println("现对象:" + studentVoList);
}
public static StudentVo changeObject(StudentVo object) {
object.setStudentName("雪容融");
return object;
}
public static List<StudentVo> changeList(List<StudentVo> object) {
object.set(0, new StudentVo("冰墩墩和雪容融", 3, 2));
return object;
}
}
输出结果:
==========对象改变属性值===========
原对象:StudentVo(studentName=冰墩墩, studentId=1, age=1)
方法返回值:StudentVo(studentName=雪容融, studentId=1, age=1)
现对象:StudentVo(studentName=雪容融, studentId=1, age=1)
==========ArrayList类型改变===========
原对象:[StudentVo(studentName=雪容融, studentId=1, age=1)]
方法返回值:[StudentVo(studentName=冰墩墩和雪容融, studentId=3, age=2)]
现对象:[StudentVo(studentName=冰墩墩和雪容融, studentId=3, age=2)]
当我们在使用另一个方法对某一个自定义类型进行修改的时候,修改了它们的实际的对象,也就是修改了原本的对象。
还有一种foreach的情况,当循环遍历时,Integer和String:
import java.util.Arrays;
public class IntegerCirculation {
public static void main(String[] args) {
Integer[] integers = {3, 9};
for (Integer integer: integers) {
integer += 2;
System.out.println(integer);
}
System.out.println(Arrays.toString(integers));
String[] list = {"hello", "hello"};
for (String string : list) {
string += "kk";
System.out.println(string);
}
System.out.println(Arrays.toString(list));
}
}
输出:
5
11
[3, 9]
hellokk
hellokk
[hello, hello]
说明String类的list和Integer类的LIst没有发生改变。
自定义类的循环遍历:
public class StudentVoCirculation {
public static void main(String[] args) {
List<StudentVo> studentList = new ArrayList<>();
studentList.add(new StudentVo("雪容融",2,2));
studentList.add(new StudentVo("冰墩墩",1,1));
for (StudentVo vo : studentList) {
vo.setAge(21);
}
System.out.println(studentList);
}
}
输出结果:
[StudentVo(studentName=冰墩墩, studentId=1, age=21), StudentVo(studentName=雪容融, studentId=2, age=21)]
说明list中的对象已经改变。