加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

如何将Integer包装类传递给java中的方法?

发布时间:2020-12-15 05:22:58 所属栏目:Java 来源:网络整理
导读:在 Java中的Ex: class A { private Integer x = new Integer(0); public void setValue(Integer q) { q = 20; } public void callX() { setValue(x); // this does not set x to be 20,which is what i need. Is there a way? }} 解决方法 你不能. 包装器类
在 Java中的Ex:

class A {
    private Integer x = new Integer(0);

    public void setValue(Integer q) {
        q = 20;
    }

    public void callX() {
       setValue(x);        // this does not set x to be 20,which is what i need. Is there a way?
    }
}

解决方法

你不能.

包装器类型是不可变的,因此它们有效地模拟基本类型的行为:通过执行q = 20,您使参数q指向具有值20的整数新的intstance,但它不会更改调用方法中x引用的原始实例.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读