Java引用类型值传递

引用类型值传递

@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));


    }
  }

在这里插入图片描述

综上可知,引用的是同一个对象,两个变量值一定是相等的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值