Immutability of string

Immutability of string

Mutable objects are those objects whose contents can be modified. Immutable objects are those objects, once created can not be modified. String class object is immutable that means when we create an object, it never changes in the existing object.

Let us see immutability of string from given program

Program
class test
{
public static void main(String arg[])
{
String s=new String(“hello”);
s.concat(“java”);
System.out.println(s);
}
}

Output:
hello

Leave a Reply

Your email address will not be published. Required fields are marked *