freetimestudys@gmail.com          +91-7097633380
  Login Here                        

Immutability of String

        Objects are broadly classified as mutable and immutable objects. Mutable objects are those objects whose contents can be modified. Immutable objects are those objects, once created can not be modified. String class objects are immutable. Once string object is created its data or state can't be changed but a new string object is created. Let us take a program to understand whether the string objects are immutable or not

Java Images

Immutability of String

class Test
{
public static void main(String[] args)
{
String str1=new String("freetimestudy.com");
str1.concat("hi"); 
System.out.println(str1);
String str2=str1.concat("hi");
 System.out.println(str2);
}
};

intput and output

output:freetimestudy.com,,freetimestudy.comhi