Java中引用之间的关系及内存分析

package test.basic;

public class TestBirth {
	public static void main(String[] args) {
		TestBirth tb = new TestBirth();
		int date=9;
		BirthDay b1=new BirthDay(1, 1, 1999);
		BirthDay b2=new BirthDay(2, 2, 2000);
		//1-1-1999
		b1.display();
		//2-2-2000
		b2.display();
		//date的值不会产生改变,只作为参数传递使i等于date的值
		tb.change1(date);
		//b1的值不会改变,b1只作为参数传递时b指向b1所引用的对象
		tb.change2(b1);
		//b2的值将改变,b指向了b2所引用的对象,调用BirthDay类的成员方法,将改变对应参量
		tb.change3(b2);
		//1-1-1999
b1.display();

		//2-4-2000
		b2.display();
		
	}
	public void change1(int i){
		i=1234;
	}
	public void change2(BirthDay b){
		b=new BirthDay(3,3,2001);
	}
	public void change3(BirthDay b){
		b.setMonth(4);
	}
}
class BirthDay{
	private int month;
	private int day;
	private int year;
	
	public BirthDay(int month, int day, int year) {
		this.month = month;
		this.day = day;
		this.year = year;
	}
	public void display(){
		System.out.println(day+" "+month+" "+year);
	}
	public int getMonth() {
		return month;
	}
	public void setMonth(int month) {
		this.month = month;
	}
	public int getDay() {
		return day;
	}
	public void setDay(int day) {
		this.day = day;
	}
	public int getYear() {
		return year;
	}
	public void setYear(int year) {
		this.year = year;
	}
	
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值