1

Might be silly question,

Need understanding on below

1.) // reference declared , null Object

String message = null;  

in above case, when will the reference 'message' eligible for garbage collection, read somewhere if make string reference to null it becomes eligible for GC

2.) // reference declared , empty String Object

String message = ""; 

3.) // reference declared , Object with value Hello

String message = "Hello";

From above, 1 references is created in all the three cases,

What about the Object creation..?? How will they be maintained in String Pool and heap

Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
  • 3
    `""` and `"Hello"` will be created as `String` objects, what do you want to know? – Smutje Dec 11 '14 at 07:58
  • @Smutje i was in a doubt, how will the objects will be created. Is `Empty String` also an object, will null be referenced somewhere, or what happens to reference as soon as i declare `String message = null; `, is it eligible for gc. – Ankur Singhal Dec 11 '14 at 07:59
  • "Is Empty String also an object" - yes, because "empty string" is just a semantic view on a string with the value `""`. "will null be referenced somewhere, or what happens to reference as soon as i declare String message = null; " - the pointer points to `null`. – Smutje Dec 11 '14 at 08:02
  • @Smutje when you say pointer points to null, what is actually null here, is it some `object` in memory..?? – Ankur Singhal Dec 11 '14 at 08:03
  • possible duplicate of [What is null in Java?](http://stackoverflow.com/questions/2707322/what-is-null-in-java) – Smutje Dec 11 '14 at 08:04

3 Answers3

2

First of all, references are not GCed Objects are. But with Strings its a bit tricky as Strings obejcts and String literals are different.

String s = "Hello";
String s1 = new String("Hello");

These are different. First one creates a String literal which goes into pool and a reference s is attached to it. Second one creates an Object of type String which points to the String literal "Hello" in pool and the reference s1 is attached tot he String object and not to the literal.

In general when an object (think as a generic object lets say Employee) is not in the live path of thread or execution flow and is not being referred by any other objects (referred doesnot mean a reference here but an association between objects like Employee and Address) it becomes eligible for GC.

Null is a special type in Java which is not subjected to GC general GC rules. Any reference not pointing to an object location is actually pointing to null.

Nazgul
  • 1,892
  • 1
  • 11
  • 15
1

in above case, when will the reference 'message' eligible for garbage collection, read somewhere if make string reference to null it becomes eligible for GC

It's not a reference. It's a local variable which could (but doesn't) contain a reference. If this variable was assigned to a non-constant String, that reference would eventually be eligible for garbage collection, once the variable is no longer referenced.

As it is though, the variable points to null, and will just vanish once the declaration scope (usually a method) is left. Nothing to garbage collect here. Garbage collection is about objects, not variables.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
0

1.) No object created. It's just reference you have defined.

2.) String object (referring to empty string "") created in string pool.

3.) String object (referring to "Hello") created in string pool.

SMA
  • 36,381
  • 8
  • 49
  • 73
  • thanks for the answer, edited my question little bit, pls have a look – Ankur Singhal Dec 11 '14 at 08:04
  • "read somewhere if make string reference to null it becomes eligible for GC" Unless and until your object holding this null object becomes eligible for GC, this string object wont be GC'd. Hope this answer's. – SMA Dec 11 '14 at 08:11