String objects are immutable in java. Explain how to create a string that can be changed A string object that is created using “String” class is immutable. The characters of the object can not be changed / modified. There are some methods, such as split(), substring(), beginsWith() etc., modifies the strings, but returns ‘new strings’. The original string will remain same. The changes are made only for the newly returned string objects.
In order to change the ‘string’ (Object of String class), it must be sent as a parameter to the StringBuffer / StringBuilder constructor. This string will be converted into the object of StringBuffer or StringBuilder object. This is the only way to change the string, but not the String class object itself.
|