• 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

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s3= new String("lkj");
String s4= new String("lkj");

here how many objects are created?i am confused a bit when string r created using new.since s3 and s4 have the same value are there 2 objects areated or s4 also points ot s3.
thanks
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two Strings are created. This is because you use the new operator.

This proves it:



But if you do this, only one String is created:



Another option is to internalize the String, which puts the new String into the String pool.

This just creates one String, even using the new operator



I hope it helps!
[ April 25, 2006: Message edited by: Edwin Dalorzo ]
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can see this for more details.

http://www.javaworld.com/javaqa/2002-09/01-qa-0906-strings.html
[ April 25, 2006: Message edited by: Leonardo Luiz ]
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s3= new String("lkj");
String s4= new String("lkj");

3 object created

object for s3 , object for s4 and the string "lkj" which created in the string pool
 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Above code will create 2 objects
1- String "jkl" which will be placed in String pool.
2- String object pointed by s3 which contain string "jkl" from string pool.


Here String object "jkl" will be reused from pool and object for s4 will be created

So there would be total 3 objects
reply
    Bookmark Topic Watch Topic
  • New Topic