java对象形参传递修改自定义对象与基本类型包装类型的不同

31 篇文章 4 订阅
21 篇文章 1 订阅

仅作为个人笔记、记录,可能会有错误,参考请慎重,欢迎指正讨论!

基本类型的包装类型的对象:

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中的对象已经改变。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

肆〇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值