• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String objects

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will the literals on line 3 that you create p and o be new string objects in memory?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, because they are character literals (they have single quotes, not double quotes).
 
Atul Chandran
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Fran Kindred
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Fran Kindred
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For SCJP purposes you don't have to worry about string literals being garbage collected.
 
Fran Kindred
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But is my explanation correct?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic