Java Quick Reference
  Language Fundamentals
  Operators and Assignments
  Flow Control and Exceptions
  Declarations and Access Control
  Garbage Collection
  Overloading and Overriding
  Threads
  The java.lang Package
  The java.util Package
  The java.awt Package
  The java.io Package
  References
  Miscellaneous Notes
  Tips & Traps
  Mock Exams

The java.lang Package Certification - String Immutability

  • String objects are read-only or immutable ie the contents of a String object never change
String str = "Hello";
str = "Goodbye";
  • in the above example, the second assignment of "Goodbye" to String, what actually happens is that a new string "Goodbye" is created and the object reference of the new string is stored in the variable str
  • operations that seem to modify a String object actually create new read-only String objects; leaving the original object unchanged
  • the StringBuffer class provides mutable or flexible string handling

Also see

String literals
Main Classes Wrapper Classes Math Class String Immutability String Class StringBuffer Class