Why Java String is mutable

8 08 2014
  • Java String is mutable, once string is initialized , we can not change the value for that reference value.
  • Reasons:
    • Mostly String is used for file name and DB connections and URL’s
    • so those value’s we should not change
    • due to that security reason String is mutable.
  • For Example:
    • public class MyString {

      public static void main(String[] args) {

      String str=”Hello”;

      System.out.println(str);

      str.concat(“My Concatination”);

      System.out.println(str);

      str=str.concat(” World”);
      System.out.println(str);

      }

      }

Output:

Hello
Hello
Hello World

 

 

 

 

 

 








%d bloggers like this: