How to prove String is immutable programmatically

How to prove String is immutable programmatically

Approach

Java


public class StringImmutableCheck {
    public static void main(String[] args) {
        String str1 = "Java";
        String str2 = "Java";
        System.out.println("Before Modification");
        if (str1 == str2) {
            System.out.println("Both pointing to the same reference");

        } else {
            System.out.println("Both are pointing to different reference");
        }
        str1 = str1 + "Hello";
        System.out.println("After Modification");
        if (str1 == str2) {
            System.out.println("Both pointing to the same reference");

        } else {
            System.out.println("Both are pointing to different reference");
        }
    }
}


No comments:

Post a Comment