java垃圾收集和空引用 [英] java garbage collection and null reference

查看:147
本文介绍了java垃圾收集和空引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class CardBoard {
Short story = 200 ;
CardBoard去(CardBoard cb){
cb = null;
return cb;

public static void main(String [] args){
CardBoard c1 = new CardBoard();
CardBoard c2 =新的CardBoard();
CardBoard c3 = c1.go(c2);
c1 = null;
//做东西
}}

//当达到doStuff时,有多少对象符合GC的要求?

正确答案是2,意思是 c1 及其故事 object。



当行// doStuff到达时,c3也是null。为什么它不符合GC的标准?

解决方案

c3是一个带有null引用的本地句柄,它没有指向hever指向)分配的对象。因此,GC没有任何问题。


In my studying for OCJP I came across the following question:

class CardBoard {
           Short story = 200;
           CardBoard go(CardBoard cb) {
                cb = null;
                return cb;
           }
           public static void main(String[] args) {
             CardBoard c1 = new CardBoard();
             CardBoard c2 = new CardBoard();
             CardBoard c3 = c1.go(c2);
             c1 = null;
            // do Stuff 
}}

When //doStuff is reached, how many objects are eligible for GC?
The correct answer is 2, meaning c1 and its story object.

When line //doStuff is reached, c3 is also null. Why isn't it eligible for GC too?

解决方案

c3 is a local handle with a null reference, it does not point (and hever has pointed) to an allocated object. Therefore there's nothing to GC.

这篇关于java垃圾收集和空引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆