基本类型——传值;引用类型——传引用

1)基本数据类型传值,对形参的修改不会影响实参;
2)引用类型(对象或数组)传引用,形参和实参指向同一个内存地址(同一个对象),所以对参数的修改会影响到实际的对象;
3)String, Integer, Double等immutable的类型特殊处理,可以理解为传值,最后的操作不会修改实参对象

------------------------------------------------------------------------------------------------------------

基本类型的变量保存原始值,即它代表的值就是数值本身;
而引用类型的变量保存引用值,"引用值"指向内存空间的地址,代表了某个对象的引用,而不是对象本身,对象本身存放在这个引用值所表示的地址的位置。

基本类型包括:byte,short,int,long,char,float,double,Boolean,returnAddress,
引用类型包括:类类型,接口类型和数组

基本数据类型在声明时系统就给它分配空间:
int a;
a=10;  //正确,因为声明a时就分配了空间

引用则不同,它声明时只给变量分配了引用空间,而不分配数据空间:
Date date;  //执行实例化,开辟数据空间存放Date对象,然后把空间的首地址传给today变量 
date=new Date();  //如果注释掉这步操作,会报错:The local variable date may not have been initialized,也就是说对象的数据空间没有分配
(详见java内存模型)

值传递:

方法调用时,实际参数把它的值传递给对应的形式参数,函数接收的是原始值的一个copy,此时内存中存在两个相等的基本类型,即实际参数和形式参数,后面方法中的操作都是对形参这个值的修改,不影响实际参数的值。

引用传递:

也称为传地址。方法调用时,实际参数的引用(地址,而不是参数的值)被传递给方法中相对应的形式参数,函数接收的是原始值的内存地址;在方法执行中,形参和实参内容相同,指向同一块内存地址,方法执行中对引用的操作将会影响到实际对象。

 

一个例子(int a, String str, int arr[])三者比较

(static写法)

package jianzhioffer;

public class Solution {
	public static void solve(int a, String str, int arr[]) {
		a ++;
		str = "macbook";
		arr[1] = 5;
	}
	public static void main(String[] args) {
		int a = 10;
		String str = new String("dellxps");
		int arr[] = {1,2,3};
		solve(a, str, arr);
		System.out.println(a + "\n" + str);
		for(int i=0; i<3; i++) {
			System.out.print(arr[i] + " ");
		}
	}
}
/* 
10(没变)
dellxps(没变)
1 5 3 (变了)
*/

(对象写法)

package jianzhioffer;

public class Solution {
	int a = 10;
	String str = new String("dellxps");
	int arr[] = {1,2,3};
	public void solve(int a, String str, int arr[]) {
		
		若:
		a ++;
		str = "macbook";
		/* 
		10(没变)
		dellxps(没变)
		1 5 3 (变了)
		*/
		
		若改为:
		this.a = a + 1;
		this.str = "macbook";
		/* 
		11(变了)
		macbook(变了)
		1 5 3 (变了)
		*/
		
		arr[1] = 5;
	}
	public static void main(String[] args) {
		Solution solu = new Solution();
		solu.solve(solu.a, solu.str, solu.arr);
		System.out.println(solu.a + "\n" + solu.str);
		for(int i=0; i<3; i++) {
			System.out.print(solu.arr[i] + " ");
		}
	}
}

再看一个例子,分别传参int和对象类型

class MyObj{
    public int b=99;
}

public class ReferencePkValue2 {
    public static void main(String[] args) { 
        ReferencePkValue2 t = new ReferencePkValue2(); 

        int a=99; 
        t.test1(a);//这里传递的参数a就是按值传递 
        System.out.println(a);

        MyObj obj=new MyObj(); 
        t.test2(obj);//这里传递的参数obj就是引用传递
        System.out.println(obj.b);
    } 
    public void test1(int a){ 
        a=a++;
        System.out.println(a);
    } 
    public void test2(MyObj obj){ 
        obj.b=100;
        System.out.println(obj.b);
    }
}

输出是:
99         
99            
100          
100           
//int值没有发生变化,但是在test2方法中对obj类做的修改影响了obj这个对象

再看一个例子,String和StringBuffer

这里要特殊考虑String,以及Integer、Double等几个基本类型包装类,它们都是immutable类型
因为没有提供自身修改的函数,每次操作都是新生成一个对象,所以要特殊对待,可以认为是和基本数据类型相似,传值操作。

public class ReferencePkValue1 {
    public static void main(String[] args){
        ReferencePkValue1 pk=new ReferencePkValue1();
        //String类似基本类型,值传递,不会改变实际参数的值
        String test1="Hello";
        pk.change(test1);
        System.out.println(test1);
         
        //StringBuffer和StringBuilder等是引用传递
        StringBuffer test2=new StringBuffer("Hello");
        pk.change(test2);
         
        System.out.println(test2.toString());
    }
     
    public void change(String str){
        str=str+"world";
    }
     
    public void change(StringBuffer str){
        str.append("world");
    }
}
输出是:
Hello        
Helloworld             
对String和StringBuffer的操作产生了不同的结果。

http://www.cnblogs.com/binyue/p/3862276.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值