java - linked list (node) scope and memory allocation or garbage collection after method returns -



java - linked list (node) scope and memory allocation or garbage collection after method returns -

maybe out of mind right now, have question scope, or memory allocation, or garbage collection.

the class linkedintlist has private field, front, variable of listnode type (line 4). rest of linked list constructed calling add() function (line 29).

question: when add() returned (line 39), listnode created in add(), new, out of scope , supposed garbage collected? or reserved because front end chain-pointing them?

1 // simple first version of linkedintlist constructor 2 // , methods add together , tostring. 3 4 public class linkedintlist { 5 private listnode front; // first value in list 6 7 // post: constructs empty list 8 public linkedintlist() { 9 front end = null; 10 } 11 //.... //.... 28 // post: appends given value end of list 29 public void add(int value) { 30 if (front == null) { 31 front end = new listnode(value); 32 } else { 33 listnode current = front; 34 while (current.next != null) { 35 current = current.next; 36 } 37 current.next = new listnode(value); 38 } 39 } 40 }

when add() returns, listnode created , appended list reserved chain pointed front. parameters passed function value go out of scope when function returns (like value) in case , cannot referred outside. listnode new object creation, allocated space in memory , remains when function returns.

the garbage collection done when references lost memory location. while programme runs references live cannot garbage collected.

java scope linked-list

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -