• 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

Strings and their constant pool

 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Am I understanding this right?

The String-constant-pool is in heap memory.

String s = �abc� ;

Creates a String literal �abc� in the String-constant-pool with s referencing �abc�.


String s1 = �abc� ;

No new String literal is created. s1 is simple referred to the already existing �abc� in the pool.


String s2 = new String( �abc� ) ;

A new String object �abc� is placed on the heap. No new String literal �abc� is put in the pool since �abc� already exist. s2 references the String object �abc� on the heap, but not the String literal �abc� in the pool.


String s3 = new String( �xyz� ) ;

A new String object �xyz� is placed on the heap and a String literal �xyz� is put in the pool. s3 references the String object on the heap, but not the String literal in the pool.


String s4 = new String( �xyz� ) ;

Another, different, String object �xyz� is placed on the heap. The if ( s3 == s4 ) fails. Again no new String literal �xyz� is put in the pool since �xyz� already exist. s4 references the String object �xyz� on the heap, but not the string literal �xyz� in the pool.


One last thing. Why doesn�t, �System.out.println( s1 + � and � + s3 ) ; � create new Strings without references? Wouldn�t the following Strings be created? � and � for one String. �abc and � for a second String. �abc and xyz� for a third String.

Thanks,

Richard
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems right to me, at least thats how i understand it.

Regarding
System.out.println( s1 + � and � + s3 ) ;
" and " will surely be created in the string pool.
I do assume that �abc and � and �abc and xyz� would be created because of left associativity and since one of them is a reference, they would be created on the heap.
 
Richard Boren
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Posted by swarna

Regarding
System.out.println( s1 + � and � + s3 ) ;
" and " will surely be created in the string pool.
I do assume that �abc and � and �abc and xyz� would be created because of left associativity and since one of them is a reference, they would be created on the heap.



Why don't we count them when counting the number Strings created?

Richard
 
swarna dasa
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We don't?

Not too sure, perhaps the question might always be how many strings created prior to line X, and most of the questions don't include System.out.println statements.

Just to be sure i looked up K&B

Here is an excerpt


String s1 = "spring ";
String s2 = s1 + "summer ";
s1.concat("fall ");
s2.concat(s1);
s1 += "winter ";
System.out.println(s1 + " " + s2);
What is the output? For extra credit, how many String objects and how many
reference variables were created prior to the println statement?

Answer:- There were a total of eight String objects



Assume that if println statements were considered we would have 3 more objects created.
And obviously we solve all these questions with the assumption that the string pool is empty.
 
Richard Boren
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


by swarna,

...how many String objects and how many reference variables were created prior to the println statement?



Thanks Swarna.
I didn't read "prior" until you pointed out.

Richard
 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>> Assume that if println statements were considered we would have 3 more objects created.

I can see only two new String objects created:

this code:

looks in byte-code this way:


where I see, that new string constant for space " " string is loaded (1st string object), and after string is build using StringBuilder, the result is put to new string Object via toString() (2nd string object)
 
Richard Boren
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


by John Stone

I can see only two new String objects created:



John I believe you are right. System.out.println() smartly uses StringBuilder (mutable).

Thanks for pointing that out.

Richard
 
Watchya got in that poodle gun? Anything for me? Or this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic