+ 73

Is string Mutable or Immutable in java?and Why it is??

15th Jan 2018, 12:15 PM
sarvesh hardikar
sarvesh hardikar - avatar
87 Réponses
+ 65
Immutable. Explanation:-) A mutable string can be changed, and an immutable string cannot be changed. Here I want to change the value of String like this, String str="Good"; str=str+" Morning"; and other way is, StringBuffer str= new StringBuffer("Good"); str.append(" Morning"); In both the cases I am trying to alter the value of str. Can anyone tell me, what is difference in both case and give me clear picture of mutable and immutable objects. Refer these links:-) https://www.programcreek.com/2013/04/why-string-is-immutable-in-java/ https://www.javatpoint.com/immutable-string
15th Jan 2018, 12:42 PM
Abhivarshini Maddala
Abhivarshini Maddala - avatar
+ 54
immutable because the value of the String is stored in a variable that is final 🤗
18th Jan 2018, 9:26 AM
NimWing Yuan
NimWing Yuan - avatar
15th Jan 2018, 3:42 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 35
-->>String are immutable in Java because they provide no methods that modify the state of an existing String object. They only provide methods that create new String objects based on the content of existing ones. ...😊😊 Hence strings are immutable because it's contents are never changed..😊😊 -->>the most important difference between String and StringBuffer/StringBuilder in java is that String object is immutable whereas StringBuffer/StringBuilder objects are mutable.😎😎😎
15th Jan 2018, 3:37 PM
🌛DT🌜
🌛DT🌜 - avatar
+ 23
immutable, stringbuilder is mutable
15th Jan 2018, 12:20 PM
Vukan
Vukan - avatar
+ 20
String str="Good"; str=str+" Morning"; ends up with 3 strings stored. The two seen above and "Good Morning". The StringBuffer version only has 2 strings. The "Good Morning" isn't stored as a string. Instead, it is a char array containing the characters.
15th Jan 2018, 1:07 PM
John Wells
John Wells - avatar
+ 19
Just in case if this isn't noticed. https://www.sololearn.com/Discuss/570524/?ref=app
15th Jan 2018, 3:53 PM
Dev
Dev - avatar
+ 18
Strings are magical things in Java. To fully understand mutable versus immutable you first must understand the parts of the language that you are looking at. String myString = "Hello"; Actually creates an instance of a String Object (with value Hello) and stores it in a memory location. myString is simply a reference variable pointing to that stored memory location. If you were to create a second string object: String myString2 = "Hello"; BOTH Object reference variables would point at the SAME instance in memory. Therefore the "String object instance itself" (aka value being referenced) IS immutable (unchangable). It (the string literal) will only be created ONCE in memory regardless of how many object reference variables reference it. That all said, the variable that is pointing to the String instance IS mutable and can be reassigned to any other string of value. i.e. myString = "New different String literal to be stored in memory". That is what is meant by the somewhat confusing statement that strings are immutable.
15th Jan 2018, 3:50 PM
Donald Hoyt
Donald Hoyt - avatar
+ 17
String is immutable in java..... when we declare string variable we assign some value to it and if we not assign then null is considered because it is reference type. when we declare any string in java first jvm check it is available on heap pool or not if available then new object is not created if not available then new object is created for that data. Ex: String s1="hello"; s1.concate("123"); System.out.println(s1); //print hello //-->immutable check this code: https://code.sololearn.com/cRFSi8C88PYG/?ref=app stackoverflow: https://stackoverflow.com/questions/22397861/why-is-string-immutable-in-java
15th Jan 2018, 11:20 PM
ASIF BILAKHIYA
ASIF BILAKHIYA - avatar
+ 15
I didn't know it, but I know now that immutable
15th Jan 2018, 4:09 PM
Bohdan Sakovych
Bohdan Sakovych - avatar
+ 14
we have 3 string classes 1.string class 2.stringbuffer class 3.stringbuilder clas in that string class is immutable and remaining 2 is mutable why means immutable means we cant do modification we can calculate min, max, len etc., we cannot modify the string value if we want to do modification also it will create duplicate string and copy the elements into it then original string deleted and duplicate string will become main string
15th Jan 2018, 4:00 PM
gnaneswari kolathuru
gnaneswari kolathuru - avatar
+ 13
IMMUTABLE; The strings in java don't have any option for the modification IN CASE, you want it to work as Mutable you just have to change the values using method of assigning new string value Note: The only thing which is MUTABLE in java is "STRINGBUFFER" or "STRINGBUILDER"
15th Jan 2018, 3:59 PM
Akash Kumar
Akash Kumar - avatar
+ 12
String literals are immutable. String s="Good"; // string literal String objects are mutable. String s=new String("Good"); //string object When you create a String literal jvm first look for the string in a part of memory called Pool. If string founds in the pool it returns the reference of that string and not create the new object. String s="Good"; // new string object created String t="Good"; //Both t and s refer to same object So, it means more than one reference variable can points to the same string literal unknowingly. So, to avoid String getting modified by one of the variables, Java doesn't allow String literal to ve mutable. When you try to change the literal new String is created and its reference is returned. But when you create String object using 'new' keyword, new String object is created everytime. PS: Hope it helps.
15th Jan 2018, 4:12 PM
Nikhil Sharma
Nikhil Sharma - avatar
+ 10
when u dont know java and cannot say anything about it(😯
15th Jan 2018, 4:54 PM
Botol
Botol - avatar
+ 8
immutable
15th Jan 2018, 3:31 PM
Elizabeth muli
Elizabeth muli - avatar
+ 8
Imutuable and Congratulations for the Question of the day.👏👏👏
15th Jan 2018, 3:53 PM
Utkarsh Singh
Utkarsh Singh - avatar
+ 8
Strings are immutable. This means they can not be altered. Actually, due to Immutable nature Strings have some(limited as compared to other) applications in programming where programmer don't wish to change value of string. StringBuffer is mutable which has wide applications in java programming because of it's mutability. The value can be change at anytime.
15th Jan 2018, 4:25 PM
Hrishikesh Kulkarni
Hrishikesh Kulkarni - avatar
+ 8
The string data type is usually immutable, but String has methods that can remove spaces, insert other registers, and so on. It all depends on the situation)
15th Jan 2018, 4:26 PM
Anathelo
Anathelo  - avatar
+ 8
String str="Good"; In this case, object created is stored in string constant pool where if we create another object with same I'd it's reference is created.So,it's immutable. StringBuffer str=new StringBuffer("Good"); And here,the object is stored in heap area and if we create another object with same I'd the old reference is lost and new one will be created.so,it's mutable.
15th Jan 2018, 4:30 PM
uday samineni
uday samineni - avatar
+ 8
Absolutely positive ! there is two main approach : if you are just developer don't mind ... go ahead if you are software engineer see this : https://www.tutorialspoint.com/data_structures_algorithms/heap_data_structure.htm next pivot to this : https://www.javatpoint.com/immutable-string but i think : it is philosophical practice not practical :)
15th Jan 2018, 5:11 PM
Amir
Amir - avatar