What is the difference between initializing a string as
I have studied that strings are immutable. So, if we create a string object, is it possible to retrieve it through garbage collection ? Thanks in advance for a reply Sridhar
Ambapali Pal
Ranch Hand
Joined: Dec 17, 2002
Posts: 47
posted
0
What is the difference between initializing a string as code: -------------------------------------------------------------------------------- String s = "sridhar" ;//1 String s1 = new String("sridhar");
In the first case no new object is created . s is pointed to sridhar string literal which is added to the JAVA's string literal pool at compile time. 2nd case sridhar is created as a new object in the runtime(even if sridhar exists at the JAVA's string pool)and referenced by s1. Hope it helps.
Sridhar Srikanthan
Ranch Hand
Joined: Jan 08, 2003
Posts: 366
posted
0
Dear Ambapali Pardon me for any ignorance but I asked whether the string object created by new keyword in my code is eligible for garbage collection. I have read that strings are immutable and cannot be garbage collected. Thanks Sridhar