Forums Register Login

String objects

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
Will the literals on line 3 that you create p and o be new string objects in memory?
+Pie Number of slices to send: Send
No, because they are character literals (they have single quotes, not double quotes).
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
For SCJP purposes you don't have to worry about string literals being garbage collected.
+Pie Number of slices to send: Send
But is my explanation correct?
+Pie Number of slices to send: Send
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.
Hey cool! They got a blimp! But I have a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 710 times.
Similar Threads
Garbage for objects and strings ...
String -query
equals and ==
GC- String
String is created on pool?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 06:03:28.