Monday, 18 January 2016

Strings are immutable

Que: Write a program that proves Strings are immutable.
Ans: Though there is no way to find if Strings are immutable through coding, but the code below is an attempt to show if Strings are mutable.Remember the only way to say if Strings are immutable is to see the API.
 public class ImmutableTest {
        public static void main(String args[]){
            String initial = "ABCDEFG";
            String after = initial.replace('A', 'Z');
    System.out.println("Immutable Demo \n");
            System.out.println("initial = " + initial);
            System.out.println("after= " + after);
        }
    }


OUTPUT :



No comments:

Post a Comment