This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes String Immutable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String Immutable" Watch "String Immutable" New topic
Author

String Immutable

Harshana Dias
Ranch Hand

Joined: Jun 11, 2007
Posts: 327
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.
Rok Štelcer
Ranch Hand

Joined: Nov 03, 2009
Posts: 101
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


SCJP, SCWCD
Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
But what about this:

Wouldn't this print:

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

That's just reassigning a different String object to the String reference. The String object itself cannot be changed.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
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".
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 917

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


My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String Immutable
 
Similar Threads
How to make class immutable ?
why dont we prefer to use StringBuffer
Creating our own String class?
Why String class is immutable
String, String Buffer and String Builder