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

 

 

 

 

 

 

Advertisements

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: