-5

Consider this Code

String variable = "";
variable = "3";
variable = "4";

Since string is immutable, there will be three objects created. But only the last object where the value is assigned to "4" is valid.

As per the definition, the objects used in the previous lines ( variable = "3" and variable ="") are still in the memory.

My question is , when an object is no longer in reference, can i still call it an Object or as a just un-usable memory location

sainath
  • 203
  • 2
  • 7
  • 19

4 Answers4

1

Object is always object. If it is reachable, not eligible for GC on next GC cycle. Otherwise it is eligible for GC.

kosa
  • 65,990
  • 13
  • 130
  • 167
1

For starters, String objects won't be created in the general heap, they would rather reside in the string pool. So, even if the "" and "3" are not being referred by any variable, they would still stay in the pool, waiting to be used in future (or destroyed). So no, they are not in a unusable memory location.

To answer your question, they will still be called objects.

Pradeep Pati
  • 5,779
  • 3
  • 29
  • 43
0

i think you can still call it object as you can use it with this.variable="3"; maybe

Sahil Pasricha
  • 135
  • 1
  • 3
  • 10
0

They are no longer referenced by the code, and really are just a pattern of bytes at a location in memory, but they are still objects. The GC has to get a handle on the object so that it can finalize it. So they still 'function' as objects...

See: When is the finalize() method called in Java?

Community
  • 1
  • 1
Noah
  • 1,966
  • 1
  • 14
  • 29