s*i 发帖数: 31 | 1 In c or c++, if given a node in a linked list, and you want to delete the
node after it. You can just:
obsolete_node = node->next
node->next=node->next->next
free(obsolete-node) //or "delete obsolete_node"
But java has garbage collection, and no such keyword as free or delete. So
in java, can i simply skip the last statement, and it'll be correct? Because
obsolete_node no longer has any references to it, so the garbage collector
will free up its memory ? | g*****g 发帖数: 34805 | 2 You are right.
Because
collector
【在 s*i 的大作中提到】 : In c or c++, if given a node in a linked list, and you want to delete the : node after it. You can just: : obsolete_node = node->next : node->next=node->next->next : free(obsolete-node) //or "delete obsolete_node" : But java has garbage collection, and no such keyword as free or delete. So : in java, can i simply skip the last statement, and it'll be correct? Because : obsolete_node no longer has any references to it, so the garbage collector : will free up its memory ?
| B********e 发帖数: 1062 | 3 try obsolete_node = null, which will help the gc to destroy it.
Because
collector
【在 s*i 的大作中提到】 : In c or c++, if given a node in a linked list, and you want to delete the : node after it. You can just: : obsolete_node = node->next : node->next=node->next->next : free(obsolete-node) //or "delete obsolete_node" : But java has garbage collection, and no such keyword as free or delete. So : in java, can i simply skip the last statement, and it'll be correct? Because : obsolete_node no longer has any references to it, so the garbage collector : will free up its memory ?
| g*****g 发帖数: 34805 | 4 That's not neccesary since obsolete_node is a local variable.
Actually the assignment for obsolete_node is not neccesary at all.
【在 B********e 的大作中提到】 : try obsolete_node = null, which will help the gc to destroy it. : : Because : collector
| B********e 发帖数: 1062 | 5 believe or not, it's a good habit to assign unused variables to null in java.
local variables could be exceptions though.
【在 g*****g 的大作中提到】 : That's not neccesary since obsolete_node is a local variable. : Actually the assignment for obsolete_node is not neccesary at all.
| g*****g 发帖数: 34805 | 6 It's a good habit to use as least instance variables as needed in
any OO languages. Instance variables are often abused
just to pass local variables inside the class.
I don't think assigning unused variables to null is neccesary,
unless you are doing it for heavy weight resource, such as an image.
Java GC is smart enough to get the job done.
java.
【在 B********e 的大作中提到】 : believe or not, it's a good habit to assign unused variables to null in java. : local variables could be exceptions though.
| B********e 发帖数: 1062 | 7 You are smart enough to argue with me (:
【在 g*****g 的大作中提到】 : It's a good habit to use as least instance variables as needed in : any OO languages. Instance variables are often abused : just to pass local variables inside the class. : I don't think assigning unused variables to null is neccesary, : unless you are doing it for heavy weight resource, such as an image. : Java GC is smart enough to get the job done. : : java.
|
|