[学习笔记]Java的值传递和引用传递,引用传递转值传递

1值传递: int,double,Integer,Double,String

public class Test {   
	public static void main(String args[]){
		int i1 = 1;
		double d1 = 1.1;
		Integer I1 = 1;
		Double D1 = 1.1;
		String S1 = "一";
		int i2 = i1;
		double d2 = d1;
		Integer I2 = I1;
		Double D2 = D1;
		String S2 = S1;
		i2 = 2;
		d2 = 2.2;
		I2 = 2;
		D2 = 2.2;
		S2 = "二";
		System.out.println(i1+" "+d1+" "+I1+" "+D1+" "+S1);
	}
}
输出: 1 1.1 1 1.1 一


2引用传递: 对象,List,Map

class A {
	int i = 1;
	double d = 1.1;
	Integer I = 1;
	Double D = 1.1;
	String S = "一";   
}

public class Test {   
	public static void main(String args[]){
		A a = new A();
		A b = a;
		b.i = 2;
		b.d = 2.2;
		b.I = 2;
		b.D = 2.2;
		b.S = "二";
		System.out.println(a.i+" "+a.d+" "+a.I+" "+a.D+" "+a.S);
		
		List<Integer> list1 = new ArrayList<Integer>();
		list1.add(1);
		list1.add(2);
		List<Integer> list2 = list1;
		list2.remove(0);
		System.out.println(list1.size());
		
		HashMap<String, String> map1 = new HashMap<String, String>();
		map1.put("a", "a");
		map1.put("b", "b");
		HashMap<String, String> map2 = map1;
		map2.remove("a");
		System.out.println(map1.size());
	}
}  
输出: 2 2.2 2 2.2 二
  1
  1


3引用传递转值传递

class A {
	int i = 1;
	double d = 1.1;
	Integer I = 1;
	Double D = 1.1;
	String S = "一";   
}

public class Test {   
	public static void main(String args[]){
		A a = new A();
		A b = new A();
		try {
			org.apache.commons.beanutils.BeanUtils.copyProperties(b, a);
		} catch (IllegalAccessException e) {
		} catch (InvocationTargetException e) {
		}
		b.i = 2;
		b.d = 2.2;
		b.I = 2;
		b.D = 2.2;
		b.S = "二";
		System.out.println(a.i+" "+a.d+" "+a.I+" "+a.D+" "+a.S);
		
		List<Integer> list1 = new ArrayList<Integer>();
		list1.add(1);
		list1.add(2);
		List<Integer> list2 = new ArrayList<Integer>();
		list2.addAll(list1);
		list2.remove(0);
		System.out.println(list1.size());
		
		HashMap<String, String> map1 = new HashMap<String, String>();
		map1.put("a", "a");
		map1.put("b", "b");
		HashMap<String, String> map2 = new HashMap<String, String>();
		try {
			org.apache.commons.beanutils.BeanUtils.copyProperties(map2, map1);
		} catch (IllegalAccessException e) {
		} catch (InvocationTargetException e) {
		}
		map2.remove("a");
		System.out.println(map1.size());
	}
} 
输出: 1 1.1 1 1.1 一
  2
  2




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值