• 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 Immutable

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

We know that Strings are immutable and if you want to change the original String value we have to use StringBuffer or StringBuilder class right?
But String class is final which make sense with immutability but why the StringBuffer and StringBuilder classes are also final?
Doesn't it have a impact on sting non immutability on those classes or it just make us not extend those classes?

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

StringBuffer and StringBuilder are more or less only "wrappers" for String, thus quite fundamental.
I guess that the only reason why they were made final is because of the security and performance.


Regards,
Rok
 
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But what about this:

Wouldn't this print:

 
Sheriff
Posts: 22783
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
That's just reassigning a different String object to the String reference. The String object itself cannot be changed.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It might be better to think of String and StringBuilder as two different ways to handle the same data. One is for storage of immutable data and the other for manipulation of data we want to change. But both store "writing".
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your statement is flawed:

We know that Strings are immutable and if you want to change the original String value we have to use...



Let me Quote my belief:

Strings are immutable and if you have the requirement to have an optimised approach towards using a
String with multiple operations of modifications being done on it, you would probably want to use a StringBuffer (or StringBuilder or MySomeWakyClassThatHandlesStringsWell)

By the way, a String is Not a StringBuffer

ie:

should not compile
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic