创建第四个符合GC条件的对象?

时间:2012-10-23 13:05:54

标签: java garbage-collection

01 class Account { Long acctNum, password;}
02 public class Banker {
03  public static void main(String[] args){
04      new Banker().go(); //created object
05      //Here there are 4 objects eligible to GC
06  }
07  void go(){
08      Account a1 = new Account(); //created object
09      a1.acctNum = new Long("1024"); //created object
10      Account a2 = a1;
11      Account a3 = a2;
12      a3.password = a1.acctNum.longValue();
13      a2.password = 4455L;
14  }
15 }

在第13行创建了一个long并且当autobox使包装器Long时,可能是第四个创建的对象?

以下行是否也在创建对象?

long l = 4455L;
long m = 4455;
long n = 4455l;

2 个答案:

答案 0 :(得分:2)

Long l = 4455L;

自动装箱并创建对象(就像a2.password = 4455L;一样)。 w ^

以下没有(因为类型是原始的,所以不需要自动装箱)

long l = 4455L;

答案 1 :(得分:1)

是的,你是对的,第13行确实通过自动装箱创建了一个新的Long。其他3行(l,m,n)不创建对象,因为它们是基元。

所以你的4个对象是Banker,Account和两个Longs。

相关问题