引用类型值传递
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class People {
@Excel(name = "年龄",width = 15D)
private int age;
@Excel(name = "生日",width = 15D)
private String birthDay;
@Excel(name = "姓名",width = 15D)
private String name;
@Excel(name = "性别",width = 15D)
private String sex;
}
public class Test {
public static void main(String[] args) {
Map<String, People> map = new ConcurrentHashMap<>();
People people = new People();
people.setSex("1");
map.put("1", people);
People people3 = map.get("1");
System.out.println("第1组:");
System.out.println(JSON.toJSONString(people3));
people3.setName("third");
//people3的引用和map.get("1")是一个
System.out.println(JSON.toJSONString(map.get("1")));
System.out.println(people3.equals(map.get("1")));
People people1 = new People();
People people2 = new People();
people2.setName("second");
people1 = people2;
System.out.println("第二组:");
System.out.println(JSON.toJSONString(people1));
System.out.println(JSON.toJSONString(people2));
System.out.println(people1.equals(people2));
people1.setName("first");
//此时name都是first。
System.out.println("第三组:");
System.out.println(JSON.toJSONString(people1));
System.out.println(JSON.toJSONString(people2));
System.out.println(people1.equals(people2));
people1 = People.builder().name("fourth").build();;
//此时people1的引用指向的是另一个对象
System.out.println("第四组:");
System.out.println(JSON.toJSONString(people1));
System.out.println(JSON.toJSONString(people2));
System.out.println(people1.equals(people2));
}
}
综上可知,引用的是同一个对象,两个变量值一定是相等的。