File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String Mutability Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String Mutability" Watch "String Mutability" New topic
Author

String Mutability

Pankaj Shet
Ranch Hand

Joined: Sep 08, 2006
Posts: 212

Hi friends,
I want to know why are Strings Immutable?
I know that state of String object cannot be changed.
But this is the meaning of immutability. I want to know why cant the state of String objects be changed? what is the purpose of String immutabilty?
Help is always appreciated.
Thanks and Regards,
-Pankaj.


PANKAJ SHET
B.Sc.(I.T.), S.C.J.P., S.C.W.C.D., PGDAC(CDAC)
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3575
    
  14

Mostly for ease of use and security, I figure.

Immutable types are very simple, and make it much easier for programmers to reason about the correctness of a program. You can easily pass Strings in and out of methods without worrying that internal state of an object is compromised, or that the String changes halfway through the execution of a method operating on it. Immutable types make it harder for a program to break, or for a malicious person to break it.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: String Mutability
 
Similar Threads
String
String replacement
Strings
Strings and StringBuffer
Stateful Session bean and Session