• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

String Constant Pool

 
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I have a doubt regarding String Object creation.
Suppose the code goes like this-
String s="abc";
String str=new String("abc");

Now ,I just want to know how many objects are created,and what is the difference between the two methods?

Thanks...
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
two objects. you get the string "abc" in the string pool.

ANY time you use the 'new' operator, a new object is created in the heap, so a new string object is created that has the same logical value of "abc", however they are two distinct objects. you can prove this by testing them with the == operator. for your s and str, it will be false.
 
Ranch Hand
Posts: 148
Hibernate Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The first statement create a String object and put in a String pool.

The second statement creates the two string object 1.On is in string pool after 2.Another on the heap.



Hope this helps
Thanks
 
Sudhanshu Mishra
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks,Fred
It was helpful.
Let me narrate it my self once.For any string literal,the compiler creates an object in string constant pool,and in this case "abc " is created in string constant pool,and 's' refers to it.
str also refers to an object in the heap,and that object too has value "abc",but now as "abc "already exists in the pool,no new object for "abc "is created.

Please acknowledge if i am getting the concepts right,or else,please do correct me.

Thanks....
 
Sudhanshu Mishra
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to you too Vijay.
 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure of the exact order, but I think each time a literal is encountered (anything in quotes), it is checked to see if it is already in the string pool. If not, it gets created there, and if so, it is not.

Then, each time you do something like "new String()", you will create a new object. You are allowed to pass a string to a String constructor, and the constructor uses that to build the 'value' of the new string.

Note that when you do stuff like this:


I think you actually end up with 7 objects.
on the String Pool:

"Fred"
"Rosenberger"
"Hello "
" "


dynamically built and eventually lost:

"Hello Fred"
"Hello Fred "


dynamically built and saved to a reference:

"Hello Fred Rosenberger"



 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:dynamically built and eventually lost:

"Hello Fred"
"Hello Fred "


Except that's not how + is compiled, at least not in Java 6. There line 3 of your code is compiled into this:
Since StringBuilder uses a char[] internally, not Strings, "Hello Fred" and "Hello Fred " are never created.
 
Sheriff
Posts: 28370
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All of which tends to demonstrate that these "How many objects are created" questions are rather futile, and where strings are concerned they are more futile.
 
Marshal
Posts: 80269
430
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Search the whole of javaranch for "Strings, literally", which is an old JavaRanch journal article which will probably answer your question.
 
Hoo hoo hoo! Looks like we got a live one! Here, wave this tiny ad at it:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic