java读取bny_Java的垃圾回收和集合

List methodA() {

List all_the_stuff_to_do = new ArrayList<>();

all_the_stuff_to_do.add(new Stuff("important"));

all_the_stuff_to_do.add(new Stuff("not important"));

return all_the_stuff_to_do;

}

List methodB() {

List important_stuff_to_do = new ArrayList<>();

Stuff important = methodA().get(0);

// at this point Stuff("not important") is not reachable anymore

// since I have no reference to the list that contains it left

important_stuff_to_do.add(important);

return important_stuff_to_do;

}

void methodC() {

... do a happydance ...

List stuff_to_do = methodB();

... look sad because now I got important stuff to do ...

}

*****编辑*****

更好的澄清和简化代码

澄清:

当退出methodA()我得到了一个列表与两个对象的参考东西(“重要”)和东西(“不是我mportant“)

我将对Stuff(”important“)的引用添加到methodB()的列表中。 此时对象东西(“不重要”)无法再到达。 同样在中的列表方法A()不再可用。

但是该列表仍然包含对确实可到达的对象的引用,即材料(“重要”)。

何时会all_the_stuff_to_do列表中清除并当的东西(“不重要”)对象?

在拨打方法A后会直接吗?或者它会在方法B的末尾?或者从来没有,因为它包含对材料(“重要”)的引用直到程序结束仍然有效的对象?

2016-04-04

Xtroce

+1

您指的是哪个列表引用? 'all_the_stuff_to_do','important_stuff_to_do'或'stuff_to_do'? –

+0

“东西(2)”是什么意思?你在打什么方法? –

+0

我想你可能的意思是:'important_stuff_to_do.add(methodA()。get(i));'在methodB中的for循环中 –

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值