+ 73
Is string Mutable or Immutable in java?and Why it is??
85 ответов
+ 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
+ 54
immutable because the value of the String is stored in a variable that is final 🤗
+ 53
congrats for post of the day
//i understand its need after making some codes
https://dzone.com/articles/why-string-immutable-java
https://stackoverflow.com/questions/22397861/why-is-string-immutable-in-java
+ 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.😎😎😎
+ 23
immutable, stringbuilder is mutable
+ 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.
+ 19
Just in case if this isn't noticed.
https://www.sololearn.com/Discuss/570524/?ref=app
+ 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.
+ 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
+ 15
I didn't know it, but I know now that immutable
+ 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