Joe
Joe

Reputation: 7919

Where is created the fourth object eligible to GC?

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 }

In line 13 is created a long and when autobox make the wrapper Long, could be the forth object created?

Are the following lines also creating objects?

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

Upvotes: 1

Views: 74

Answers (2)

jalynn2
jalynn2

Reputation: 6457

Yes, you are right, line 13 does create a new Long by autoboxing. The other 3 lines (l,m,n) do not create objects because they are primitives.

So your 4 objects are Banker, Account and the two Longs.

Upvotes: 1

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

Long l = 4455L;

That autoboxes and creates object (just like a2.password = 4455L; does). W

hile the following doesn't (because the type is primitive so, there's no need to autobox)

long l = 4455L;

Upvotes: 2

Related Questions