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

String objects

Atul Chandran
Greenhorn

Joined: Oct 24, 2004
Posts: 22
Hi,
The following code will create two String objects
1. String r=new String("Hello");
One in the non pool area of memory and one in the String pool.

How many String objects will line 3. create? 1 or 2? And will the existence of "Hello" String in the pool make any difference?

2.String a="Hellp";
3.a=a.replace('p','o');

And will the existence of "Hello" String in the pool before line 1 make any difference?
Thanks in Advance,
Atul
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Look at the String API:
String replace(char oldChar, char newChar)
Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar


So on line 3. String reference a will be replaced by a reference to a new String object.
The original string "Hellp" that was referred to by a will remain unreferenced in the string literal pool.
[ November 04, 2004: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Francis Palattao
Ranch Hand

Joined: Sep 22, 2004
Posts: 91
Will the literals on line 3 that you create p and o be new string objects in memory?


SCJP 1.4 <br />SCWCD 1.4 (preparing)<br />SCBCD 1.3 (coming soon)
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
No, because they are character literals (they have single quotes, not double quotes).
Atul Chandran
Greenhorn

Joined: Oct 24, 2004
Posts: 22
Nop they are character literals...

Hi Barry,
String a=new String("Hello");//will create two objects

one in the non pool area of memory and
one is added to the String pool.

My question is whether the String "Hello" will be added to the pool if already a "Hello" object is present in the pool.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
No it will not be added. A new string object containing the characters Hello will be created on the heap, not the literal string pool.

You can see this by printing the value of a=="Hello" after the last statement.



(I replaced "a==r" by a=="Hello")
[ November 04, 2004: Message edited by: Barry Gaunt ]
Francis Palattao
Ranch Hand

Joined: Sep 22, 2004
Posts: 91
if they were to use double quotes on the literals would that entail 2 string objects then plus one that is returning from the method.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Note first that that would be a call to a different version of the replace method, the first argument of which is a regular expression.

Anyway, in that case there would be a literal pool entry for each of the two String literal arguments and one for the returned resultant string.

If you are really nuts about this sort of thing then decompiling the class with "javap -verbose" can teach you something about what happens.
Francis Palattao
Ranch Hand

Joined: Sep 22, 2004
Posts: 91
Thanks! One last question no String objects that are created using a string literal can ever be garbage collected because there will always be a reference from the string pool to the object. correct?

String a = "test";
a = null;

the "test" obj can never be garbage collected while

String a = new String("test");
a = null;

can be garbage collected.
[ November 04, 2004: Message edited by: Francis Palattao ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
For SCJP purposes you don't have to worry about string literals being garbage collected.
Francis Palattao
Ranch Hand

Joined: Sep 22, 2004
Posts: 91
But is my explanation correct?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
As far as I know your explanation is correct, provided your class is not dynamically unloaded. And now we are definitely outside the scope of the SCJP.
 
jQuery in Action, 2nd edition
 
subject: String objects
 
Similar Threads
Garbage for objects and strings ...
String is created on pool?
GC- String
equals and ==
String -query