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

5
  • 3
    "" and "Hello" will be created as String objects, what do you want to know? Commented Dec 11, 2014 at 7: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. Commented Dec 11, 2014 at 7: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. Commented Dec 11, 2014 at 8:02
  • @Smutje when you say pointer points to null, what is actually null here, is it some object in memory..?? Commented Dec 11, 2014 at 8:03
  • possible duplicate of What is null in Java? Commented Dec 11, 2014 at 8:04

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

2 Comments

thanks for the answer, edited my question little bit, pls have a look
"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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.