Java初学者问题求助!!

snowshine1985 2013-02-05 04:03:15
请问各位在BB类里定义的aa是在堆内存还是栈内存中的,请详细说说这个小程序的内存执行过程好吗?(AA类清楚) 谢谢各位!!

public class AA {
private int i;
public AA(int i){
this.setI(i);
}
public int getI(){
return this.i;
}
public void setI(int i){
this.i=i;
}
}
public class BB {
private AA aa;
public BB(AA aa){
this.setAA(aa);
}
public AA getAA(){
return this.aa;
}
public void setAA(AA aa){
this.aa=aa;
}
}
public class Main {
public static void main(String[] args){
AA x1=new AA(3);
BB x2=new BB(x1);
System.out.println(x2.getAA().getI());
}
}
...全文
215 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
飞翔的卡其啦 2013-02-06
  • 打赏
  • 举报
回复
堆内存,AA x1=new AA(3); 生成了类AA的对象,x1是该对象的引用。x1所指向的对象保存在堆内存中。 当BB x2=new BB(x1)时,只是把对类AA的对象的引用复制给了类BB的成员变量aa。 所以x1和aa都是指向一个对象,都保存在堆内存中。
xtfgy2012 2013-02-05
  • 打赏
  • 举报
回复
堆内存,因为是成员变量,是对象的一部分,分配在堆上 对象消失后,它才消失。
w774254848 2013-02-05
  • 打赏
  • 举报
回复
是栈内存、、、
maxcai 2013-02-05
  • 打赏
  • 举报
回复
觉得是stack
  • 打赏
  • 举报
回复
栈内存存放的是一些基本类型的变量和对象的引用变量; 堆内存存放是由new创建的对象和数组。

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧