Why string class is immutable in java

Give me correct answer with real time example it's very Argent to me


33 Answers
1-20 of  33
33 Answers
  • once we create a String object, we can't perform any changes in the existing object. If we try to do so, a new object will be created. this non-changeable behaviour is known as Immutability in java.
    Ex:
    String s = new String ("Hello");
    s.concat("world");
    System.out.println(s);
    output : Hello
    above example no change in String s.

  • The string is Immutable in Java because String objects are cached in String pool. ... Another reason of why String class is immutable could die due to HashMap. Since Strings are very popular as HashMap key, it's important for them to be immutable so that they can retrieve the value object which was stored in HashMap

  • Strings are immutable which means the value or data will not change. When we perform an operation on strings we cant change its value, but new object is created. 
     

  • string is immutable we all know . means we can not modify the string object in real time if we declare something as string type then main advantage is security will achieve and also thread safe will achieve means, immutable objects can not be changed, they can be shared among multiple threads freely. This eliminates the requirements of doing synchronization.

  • virangula

  • For storing in map as a key,String is given immutable..

  • suppose i have 4 objects of String literals all have string named as "hello" if i change one of the object of string then it will affects all of the strings because string literals are stored in string pool not in heap memory thats it! :)

  • Because java uses the concept of string literal.Suppose there are 5 reference variables, all refers to one object "Sachin".If one reference variable changes the value of the object, it will be affected to all the reference variables. That is why string objects are immutable in java.

  • String in java uses concept of String Pool literal. When a string is created and if the string already exists in the pool, the reference of the existing string will be returned, instead of creating a new object and returning its reference.If a string is not immutable, changing the string with one reference will lead to the wrong value for the other references.

  • Suppose you have a lock and one key, if somebody change your key, will you able to open the lock ? No, right ! Same goes here, String used as keys in Map, if somebody changes the behaviour, we won't able to get the actual value ever ! That's why String is immutable. There are so many example, this is one of them.

  • YEs....for less memory utilization..wheever we create string obj..first it will search in string pool..if same string is already present in string pool then it wont create new string obj..it just refer to the that previous obj ..we can not modify String obj...If you want to modify then go for StringBuffer or Stringbuilder

  • Because the object is created in constant pool of heap segment where duplicates are not allowed. For example when you create your account with the same name the Google will suggest you that the name is already present try with a new name.

  • Were String not immutable, a connection or file would be changed and lead to serious security threat. Mutable strings could cause security problem in Reflection too, as the parameters are strings. Efficiency The hashcode of string is frequently used in Java. For example, in a HashMap

  • Immuntable means unchangable and in string once you have created the object and if you try to change the String value then that object is not altered. If you make some changes in String and assign that string either to a new String variable or the same variable then a new object will be created and stored in that String . Example: String s=new String("AVI"); s.concat("NASH"); //no changes will be there i the object but s=s.concat("NASH"); //then new object will be created and stored in variable s

  • Yes String are Immutable in Java . Because When you are concatenating other strings to the previous string you are not changing the literal value of the previous string. EX: String str = new String("Core "); str.concat("java"); System.out.println(s); System.out.println(s); Output: Core Java

  • I aagree with the example given below, most importantly the fact is that:

    When you are Declaring String ...Memory location is being created, once that memory is filled, it cannot be changed till the programs exit.

  • In java, string objects are immutable. Immutable simply means unmodifiable or unchangeable.
    Once string object is created its data or state can't be changed but a new string object is created.

  • string a="hello"//:helo is created of the string pool
    string b="hello";//now id refer value instead of creating java .

    1
    Strings are immutable which means the value or data will not change. When we perform an operation on strings we cant change its value, but new object is created.
    Ex: 
    String s=new String("ABCD");
    s.concat("EFGH");
    System.out.println(s);
    Output:
    ABCD
    The value of string s is not changed because strings are immutable but new object will be created with string s value as "ABCDEFGH". If we assign the s value of s to a variable then the value of an is displayed in the output.
    EX:
    String s new String("ABCD");
    System.out.println("the value of s before concat is"+s);
    s=s.concat("EFGH");
    System.out.println("the value of s after concat is"+s);
    Output:
    the value of s before concat is ABCD
    the value of s after concat is ABCDEFGH
    Here new object is created with s value as ABCDEFGH.
    Since strings are immutable the value cannot change but new object will be created with changed values.

     

  • according to my point of view 
    string does not provide the mehods which are changes the content of the string. 
    the string always create a new object for every operation that we performed on the string.
    if we want to make the modifications you should convert the string into string buffer or string buiilder.
    String str="welcome";
    StringBuffer sb=new StringBuffer(str);
    so, the object covert into mutable object 
     

1-20 of  33
Core Java

Didn't get the answer.
Contact people of Talent-Core Java directly by clicking here