• 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: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

even though String is immutable which creates hell of the objects,still we use it repeatedly in our application.why?
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For numerous reasons.

Some of those:
- it's about the only thing that can be printed to the console or most user interface controls. Even primitive types such as int are converted to Strings
- because they are immutable they are perfect as keys for Maps
- it just makes sense to store (fixed) alphanumeric information in Strings

Now about the creation of objects. Sure, a lot of methods of the String class return a new String object. But a lot of the time, the original becomes available for garbage collection right afterwards. In the end, the number of String objects doesn't change that much over time in most applications.

Now there is a trick to reduce the number of String objects somewhat. When you know that you need to do a lot of String concatenation, removal or replacing, you should consider using StringBuilder. Unlike String, this is mutable, and if you are finished you can create a String using the toString method.
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
according to K&B

one of the key goals of any good programming language is to make efficient use of memory.As applications grow,it is very common for String literals occupy large amounts of a program's memory,there is often a lot of redundancy within the universe of String literals for a program. To make Java more memory efficient,the JVM sets aside a special area of memory called the String constant pool.When the compiler encounters a String literal, it checks the pool to sec 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.(The existing String simply has an additional reference.) Now we can start to see why making String objects immutable is such a good idea.If several reference variables refer to the same String without even knowing it,it would be very bad if any of them could change the String's value.
[edit]Corrected tags. CR[/edit]
[ July 17, 2008: Message edited by: Campbell Ritchie ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic