java的引用传递和值传递

package test;

import Impl.LNode;

public class testObejct {
    public static void main(String[] args) {
        Integer a=3;
        Integer b=4;
        a=b;  //这里虽然也是赋值,但是基本数据类型的包装类型 有自动拆箱机制,这里自动拆箱成了int 然后赋值,所以是值传递 
        a=6;
        System.out.println("a的值:"+a+","+"b的值:"+b);  //a 6  b 4      
       
        demo demoA=new demo();
        demoA.setDate(1);

        demo demoB=new demo();
        demoB.setDate(2);
        System.out.println("A:"+demoA.getDate()+"B: "+demoB.getDate()); // A 1  B 2


        demoA =demoB; //把B的引用给了A 导致A和B现在指向同一个对象    所以设置A的值为999后 B的值特发生变化
        demoA.setDate(999);

        System.out.println("A:"+demoA.getDate()+"B: "+demoB.getDate()); // A 999  B 999


 }

}



猜你喜欢

转载自blog.csdn.net/qq_20009015/article/details/80711056
今日推荐