java garbage collection and null reference

前端 未结 6 1780
梦谈多话
梦谈多话 2020-12-20 05:27

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

class CardBoard {
           Short story = 200;
           CardBoard go(CardBoard cb) {
                


        
6条回答
  •  南方客
    南方客 (楼主)
    2020-12-20 06:04

    Your CardBoard#go() method does nothing. It receives reference to some object, immediately forgets it, replacing by null, and returns this null value.

    So, the line

    CardBoard c3 = c1.go(c2);
    

    also does nothing, i.e. no object is created. Just null assigned to c3. So it is not garbage collected because it is already not exist.

提交回复
热议问题