Java Immutable

1.1 Introduction to Java Immutable 

Immutable means that content of the object cannot be changed once it is created. In java, String is immutable and its content cannot be changed.

By the statement “content cannot be changed” we mean that whenever you try to update the content of a string, a new object of string is created and returned.

We know there are several methods available like concat which is used to update the String but such methods returns the new String.

1.2 Example

Let’s use concat method and we can see that initial string content will remain same.

public class Test {
    public static void main(String[] args) {    
        String str = "This is the intital String ";        
        System.out.println("INITIAL STRINg="+str);        
        String str1= str.concat("APPEND");        
        System.out.println("INITAL STRING PSOT CONCAT="+str);        
        System.out.println("RETURNED STRING =" + str1);        





Like us on Facebook