Java's standard way to handle text is to use its String class. Any given String in Java is an immutable object, which means its content cannot be changed. A String has an array of characters. Whenever a String must be manipulated, any changes require the creation of a new String (which involves the creation of a new array of characters, and copying of the original array).For example
String s1=new String(”Hello”);// line
1
s1=s1+”Welcome”;// line 2
First line creates a String object in
heap area with content “Hello” and makes s1 to reference this
object.Second line creates a new String object in heap with content
“Hello Welcome” and makes s1 to reference this new object.Now
first string object with content “Hello” have no external
reference and hence it is eligible for garbage collection.