您的位置:首页 > 编程语言 > Java开发

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

2013-03-18 22:27 218 查看
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;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: