Java对象引用传递探索

一直认为本身对对象传递理解的颇为深入,没想到最近一次的编码中,就犯下了这样的错误,令本身排查了好久才找到问题的根源, 辅以小case记录以自省。java

代码以下:dom

public class ObjReference {
    String name = "ObjectReference";
    String id = UUID.randomUUID().toString();
    
    public ObjReference(){}
    
    public ObjReference(String name, String id){
        this.name = name;
        this.id = id;
    }
    
    public String toSelfAttr(){
        return "name = " + name + ", id = " + id;
    }
    
    public void fillSelf(ObjReference obj){
        /*System.out.println("old address: " + obj);*/
        obj = cloneSelf();
        /*System.out.println("after clone,it's address: " + obj);*/
    }
    
    public ObjReference cloneSelf(){
        ObjReference obj = new ObjReference(
                "cloneSelf",UUID.randomUUID().toString());
        /*System.out.println("clone ObjReference's address: " + obj.toString()
                + ", and its selfAttr: " + obj.toSelfAttr());*/
        return obj;        
    }
    
    public static void main(String[]  args){
        ObjReference obj = new ObjReference();
        System.out.println("old ObjReference's address: " + obj.toString()
                + ", and its selfAttr: " + obj.toSelfAttr());
        obj.fillSelf(obj);
        System.out.println("after filled, ObjReference's address: " + obj.toString()
                + ", and its selfAttr: " + obj.toSelfAttr());
    }
    
}

各位看官,运行结果会是如何? fillSelf()以后,对象自己属性改变是否会生效?  来看运行结果:工具

old ObjReference's address: com.chq.study.ObjReference@bb494b, and its selfAttr: name = ObjectReference, id = 91f17723-9227-461e-878e-51f7a3eedb0f
after filled, ObjReference's address: com.chq.study.ObjReference@bb494b, and its selfAttr: name = ObjectReference, id = 91f17723-9227-461e-878e-51f7a3eedb0f

咱们会发现,对象地址没有改变(这个好理解,对象是按引用传递的),但出乎我预料的,对象属性也没有任何变化.... why?this

放开fillSelf() & cloneSelf()的注释, 再次运行下,看看之间发生了什么。编码

old ObjReference's address: com.chq.study.ObjReference@1636e4e, and its selfAttr: name = ObjectReference, id = c10f9c98-8f15-4343-85db-7a85e21b22d7
old address: com.chq.study.ObjReference@1636e4e clone ObjReference's address: com.chq.study.ObjReference@df0438, and its selfAttr: name = cloneSelf, id = eb117f7a-3463-4371-b723-4f43a041018d after clone,it's address: com.chq.study.ObjReference@df0438
after filled, ObjReference's address: com.chq.study.ObjReference@1636e4e, and its selfAttr: name = ObjectReference, id = c10f9c98-8f15-4343-85db-7a85e21b22d7

橘黄色背景的,说明了最终结果没有变化。 青色背景的,说明对象在fill过程当中,实际是有变化的,不只是对象属性,其address也是发生了变化的。spa

既然address都已经变化了,那为什么最终结果并无体现出这种变化呢?这个说明了什么? 指针

你们都知道的:对象传参时,是按引用传的,这个引用,指的是指向内存堆heap中实际对象的地址,全部对此对象的改变,实际是发生在heap中的那个实际对象体块上。code

可这个解释不了示例中的现象,由于对象地址也是改变了的,虽然new了新对象,但咱们确实将新对象的address返回并覆盖原对象地址了,那为什么没有获得预期的结果?对象

你们未必知道的:对象引用传递时,对象引用自己是按值(by-value)传递的,是保存在thread stack上的,即copy了一份出来进行传递的,如同基本类型的传递。blog

因此虽然咱们明确改变了对象引用指向的heap地址,以及传递对象自己的地址(是对象自己地址的copy,如同指针),但实际对象自己地址并未改变,因此最终结果不会有变化。

这同时也是我所犯下的错误。因此若是想使用相似此种实现,有两种办法:

一、原对象不要先指向任何对象(不管new仍是null),仅声明并直接指向待构造新对象的方法便可(如: ObjReference obj2 = test.cloneSelf())  

二、改变对象的方法中,还使用原来对象,不要new新的对象出来(确保对象引用自己没有变化,变化的仅是heap中的)

咱们能够在main中屏蔽掉以前的代码,增长以下代码,进行方式1的验证:

    public static void main(String[]  args){
        /*ObjReference obj = new ObjReference();
        System.out.println("old ObjReference's address: " + obj.toString()
                + ", and its selfAttr: " + obj.toSelfAttr());
        obj.fillSelf(obj);
        System.out.println("after filled, ObjReference's address: " + obj.toString()
                + ", and its selfAttr: " + obj.toSelfAttr());*/
        ObjReference test = new ObjReference();
        ObjReference obj1 = null;
        test.fillSelf(obj1);
        System.out.println(null == obj1 ? "obj1 is null." : 
                "obj1 is : " + obj1.toSelfAttr());
        ObjReference obj2 = test.cloneSelf();
        System.out.println("obj2 is : " + obj2.toSelfAttr());
    }

运行结果:

old address: null
clone ObjReference's address: com.chq.study.ObjReference@18e261d, and its selfAttr: name = cloneSelf, id = 37be891f-127c-4b70-a992-fa842d79ca2e
after clone,it's address: com.chq.study.ObjReference@18e261d
obj1 is null.
clone ObjReference's address: com.chq.study.ObjReference@1684706, and its selfAttr: name = cloneSelf, id = efc60431-d20a-4614-83ff-d3eaa018c41c
obj2 is : name = cloneSelf, id = efc60431-d20a-4614-83ff-d3eaa018c41c

 个人一个疑问,盼高人指点: java中有能够查看对象引用自己地址(引用自己的指针)的方法或者工具么? 若有,可对此作更增强有力的支撑验证。

相关文章
相关标签/搜索