aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String objects Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "String objects" Watch "String objects" New topic
Author

String objects

Sridhar Srikanthan
Ranch Hand

Joined: Jan 08, 2003
Posts: 366
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
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
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
 
I agree. Here's the link: jrebel
 
subject: String objects
 
Similar Threads
On String.
Passed SCJP 1.2 !
Date format issue
how to display an icon along with a string in one cell of JTable
SCEA II/III passed with 92%