0

illuminati

When I set 1.next to point to 3, will the 2 be deleted because there's nothing pointing to it, or will it stay in memory because its next points to 3?

  • 1
    For the record: you are expected to do a bit of research prior posting questions. When you are starting to learn about a language, rest assured: anything you can dream of asking has been asked before. Beyond that, there are many resources out there that nicely explain how the standard Java GC mechanism(s) work. – GhostCat Dec 09 '18 at 12:31
  • Oops, sorry, I only checked the first ones that popped up when I was searching. –  Dec 09 '18 at 12:46
  • 1
    Never mind. At least you put up some effort when writing your question! – GhostCat Dec 09 '18 at 13:03

2 Answers2

2

It's unreachable, so will be collected. References only work one way in Java - given a reference to '3' you cannot get hold of '2'.

If nothing other than '1', '2' and '3' referenced '1', '2' and '3' then they would all be unreachable, even if they had cyclic references between them.

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
1

2 will be deleted by the garbage collector if nothing points to it. It will make no difference if it is there or not, when it is not reachable by any other value.

Donat
  • 4,157
  • 3
  • 11
  • 26