• 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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello Sir

My question is "how many objects are made after String1 s="abc" and String2 s=new String("abc");"
I have also read kaithy seira but I couldnt understand...
I know When the
compiler encounters a String literal, it checks the pool to see if an identical String
already exists. If a match is found, the reference to the new literal is directed to the
existing String, and no new String literal object is created.
Is it for only literals or for both when using new keword
I mean if i made a String using literal and new keyword with same string than how many objects are created.


thanks
 
Ranch Hand
Posts: 47
MyEclipse IDE Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I ll keep it simple..

Using a constructor always creates a brand new object i.e. using a constructor does not intern the string



so..you have two distinct objects

there is an error in your declaration statement
it should be


i hope this was helpful!!
 
Ranch Hand
Posts: 45
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup.. As mentioned in the above post, two distinct objects will exist. But its recommended not to create Strings with "new" constructor, as its better to reuse the "pool" of string if it exists so as to reduce the memory consumption.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harsha Ka wrote:Yup.. As mentioned in the above post, two distinct objects will exist. But its recommended not to create Strings with "new" constructor, as its better to reuse the "pool" of string if it exists so as to reduce the memory consumption.


That's true, and my suggestion is to use String.valueOf(String). In fact, I'd suggest that you always use a static factory method to construct objects if one is available.

However, the chances of using new causing problems are probably exaggerated. You'd have to be creating many thousands of Strings before it's likely to become an issue; so it's far less likely to cause you problems than using str1 == str2 incorrectly even once.

Look out for my new page on this very subject in the near future.

Winston
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic