`
spirithenry
  • 浏览: 1655 次
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

TIJ Learning:值传递与引用传递

阅读更多

 

值传递:方法调用时,实际参数把它的值传递给对应的形式参数,方法执行中形式参数值的改变不影响实际参数的值。
引用传递:也称为传地址。方法调用时,实际参数的引用(地址,而不是参数的值)被传递给方法中相对应的形式参数,在方法执行中,对形式参数的操作实际上就是对实际参数的操作,方法执行中形式参数值的改变将会影响实际参数的值。

 

public class ValuePass {

    public static void main(String[] args) {
    	//primitive type
    	int i = 1;
    	int j = 2;
    	operator(i,j);
    	
    	//String type
        String a = "a";
        String b = "b";
        operator(a, b);
        
        //String Array
        String[] array1 = new String[]{"1","2","3"};
        String[] array2 = new String[]{"4","5","6"};
        operator(array1,array2);

        
        //StringBuilder --> Object
        StringBuilder sb1 = new StringBuilder("a"); 
        StringBuilder sb2 = new StringBuilder("b");
        operator(sb1, sb2);
        
        //Object
        People p1 = new People("p1");
        People p2 = new People("p2");
        operator(p1, p2);
        
        System.out.println(i + "," + j);
        System.out.println(a + "," + b);
        System.out.println(array1[0]+array1[1]+array1[2] + "," + array2[0]+array2[1]+array2[2]);
        System.out.println(sb1 + "," + sb2);
        System.out.println(p1.name + "," + p2.name);
    }


        // int type just a value copy transfer
        public static void operator(int i, int j) {
    	  i = 3;
    	  j += 1;
	}

        // String type variable is final, change value will create a new String.
        public static void operator(String a, String b) {
            a += "aa";
            b = a;
        }
    
        //Array type variable regarded as an object, transfer copy of address
        public static void operator(String[] array1, String[] array2) {
    	    array1[0]="111";
    	    array2 = new String[]{"7","8","9"};//point to a new array
    	    array2[0] = "10";
	}

        //StringBuilder type variable is to transfer a reference of object(copy of address) from variable to parameter
        public static void operator(StringBuilder a, StringBuilder b) {
            a.append("aaa");//change value --> affect original object
        
            //point to new object --> change b not affect original object
            b = new StringBuilder("bbb");
            b.append("ccc");
        }
    
        // Object
        public static void operator(People p1, People p2) {
            //p1 is the reference of Object p1, change will affect original object
        	p1.name = "p3";
            
        	//p2 point to new address, no relationship with variable p2.
	        p2 = new People("p4"); 
	        p2.name = "p5";
	}
}

class People{
	String name;
	People(String name){
		this.name = name;
	}
}

 

1. 基本数据类型:实际传递的是数值,形参的改变不会影响实参本身。

2. String类型:String类型是final的,所有改变String的操作都会new一个新的String(新内存地址),原本的String引用则不会受到影响。

3. Array类型:数组实参到形参的传递是引用的传递,传递的是内存地址的拷贝,形参中数组每一项的改变也会影响原本数组对象。

4. StringBuilder类型:也是传递的内存地址的拷贝。

5. 对象类型:

在方法中把对象作为参数,方法调用时,参数传递的是对象的引用(内存地址),实际参数把对对象的引用(内存地址)传递给形式参数。这是实际参数与形式参数指向同一个地址,即同一个对象,方法执行时,对形式参数的改变实际上就是对实际参数的改变,这个结果在调用结束后被保留了下来。

对于对象类型,如果在方法中修改了成员变量的值,那个修改是生效的,方法调用结束后,成员变量值会改变,但是如果方法中把形参指向一个其它的对象,方法调用结束后,原来对它的引用并没用指向新的对象。

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics