+ 10

String are immutable. What is this exactly ? Can any one give a example?

Immutable means cannot modifiable. But In this Ex: String s1="ab",s2="cd"; s1=s1+s2; Here output is s1="abcd"; s1=s1.UpperCase(); Then here output is s1="ABCD"; Why not giving errors? s1 is modifying its value. Why?

2nd Oct 2019, 4:11 PM
Jayakrishna 🇮🇳
21 Réponses
+ 8
Maybe this can help you to understand in a better way https://www.javatpoint.com/immutable-string
2nd Oct 2019, 7:52 PM
A͢J
A͢J - avatar
+ 6
In c++ you can change the string s character in middle but in python u can't do it .The only way do to it is create a new string and assign it to same variable
3rd Oct 2019, 8:07 AM
Vijay(v-star🌟)
Vijay(v-star🌟) - avatar
+ 5
Strings are immutable, in the example you did not change the strings. Reassigning string variables do not change the strings. Reassigning string variable will destroy old string and replace it with a new string.
2nd Oct 2019, 4:49 PM
Seb TheS
Seb TheS - avatar
+ 3
s1 is not modifying it's values.. Recently i had to give a presentation on the same so i had found why this happens... Well in the first case s1="ab" This creates an object s1 with value ab.. This object takes some place in the heap. Now s2="cd" This creates an object s2 with value cd.. This object again takes some place in the heap. Now when you do a=a+b or a=a.concat(b) A new object is created and this again occupies more memory in the heap.. So you're not modifying the previous object instead you're creating a new object.. /*Wastage of memory*/ To avoid this we use StringBuffer/StringBuilder classes in Java 👍 Hope you got it I had referred this link: https://medium.com/@abhiroop.nray/understanding-string-in-java-31519b8d7b9c P.s to be more specific the reference variable is assigned and the stuff gets more detailed.. Let's not get there 😁
4th Oct 2019, 11:01 AM
$hardul B
$hardul B - avatar
+ 2
Jaya krishna I don't see any difference between strings and integers according immutability.
2nd Oct 2019, 7:56 PM
Seb TheS
Seb TheS - avatar
+ 2
A J thank you. The Link helped me to get clarity.. I read similar long ago. but i thought reassigning is also not possible. Gone to start point to rethinking.. But this actually leads a lot of wastage of garbage collection .. ? need to know about it.
2nd Oct 2019, 8:50 PM
Jayakrishna 🇮🇳
+ 2
jtrh Seb Thes rodwynnejones A J Thank you very much to all of you for your replays.. Please share if you any advanced info about this in security with strings and garbage collection,
2nd Oct 2019, 9:06 PM
Jayakrishna 🇮🇳
+ 2
Jaya krishna Check this code to know more about Java. https://code.sololearn.com/W71i1t1MPVp6/?ref=app
2nd Oct 2019, 9:22 PM
A͢J
A͢J - avatar
+ 2
New objects are created rather than the original being modified.
3rd Oct 2019, 2:35 AM
Sonic
Sonic - avatar
+ 2
When you think about immutability, consider that String is an object. Usually when you write a java class, it would have variables that represent state, and methods that manipulate these variables. Now in a program, when you create a string, the JVM assigns a piece of memory to it. If you use the same string multiple times (assign to different variables), for efficiency reason the JVM allocates the same memory spot that stores the string only once. This is why immutability is important, it prevents a situation when changing one string variable would impact all the other variables that might have the same string. Garbage collection is automatic in the JVM when no more variables point to that string, it is deallocated and eventually deleted from memory to free up space. Also there are mutable versions of string, called StringBuffer and StringBuilder https://www.javatpoint.com/difference-between-stringbuffer-and-stringbuilder
3rd Oct 2019, 2:44 AM
Tibor Santa
Tibor Santa - avatar
+ 1
s1 isn't modifying it's value, s1 is being reassigned to s1.toUpperCase() which is a new string of old s1 in all upper case letters https://www.programcreek.com/2013/04/why-string-is-immutable-in-java/
2nd Oct 2019, 4:16 PM
jtrh
jtrh - avatar
+ 1
So much confusing. Concept is clear to me . link provided is giving much explanation. I already studied that earlier also. But stil some confusion why string called immutable, because here s1 is reassigning, also s1 changing its value means that modifying its value ? We dont thought about string pool everytime. We thought about only string values na..!
2nd Oct 2019, 4:57 PM
Jayakrishna 🇮🇳
+ 1
Jaya krishna It's just the variable which changes. Old string will always be just replacee when you "do changes" to a string. Example { //Integers are immutable int x = 9; x += 2; } How was that possible? It changed x's value. It is possible because the initial integer (9), was not actually changed, but the variable (x) changed it's target to 9 + 2, 11, x's old value 9 and new value 11 were 2 separate objects. Variable x got reassigned to 11 during x+=2, which is just a shortcut for x=x+2. x got reassigned to 11, but the old value (9) was not changed. It works very similarly for string variables. s1.UpperCase() did not modify the original string, it just replaced it with a new string.
2nd Oct 2019, 5:18 PM
Seb TheS
Seb TheS - avatar
+ 1
Wow. Nice collection A J. Thank for sharing..
2nd Oct 2019, 9:41 PM
Jayakrishna 🇮🇳
+ 1
Look at this exemple of a lower level language, C. https://code.sololearn.com/c0rB7w0VgDuB/?ref=app You can see here that the same string defined in any point of the app has the same memory position. With this you don't waste memory declaring the same object multiple times. Because of a string is defined in many places if there was mutable a change to one will cause a change in the others, and no ones want that. That's why the strings are immutable.
4th Oct 2019, 8:24 AM
Miquel Andreu Fuster Sancho
Miquel Andreu Fuster Sancho - avatar
+ 1
I understood concept. But actually why we need to think about strings immutability? Even integer n other primitives also immutable. But we don't consider it in reality. Then y to strings, when we are getting required (reassigned) values? Why to consider the string pools also when coding? I need an example to understand this. If it is understood, have an example, please share. $hardul B thanks for the post.. Thank to all who replied..
4th Oct 2019, 3:57 PM
Jayakrishna 🇮🇳
+ 1
Integer class gives you Integer object.. It is immutable I don't think primitives can be immutable It's obvious that int i cannot be re written as i[0]...as it isn't a collection sort of variable You may simply do int i=5; Then i=9; Meanwhile see this article it says why immutable is needed. 👍 https://www.google.com/amp/s/www.javaworld.com/article/2077343/java-s-primitive-wrappers-are-written-in-stone.amp.html Also feel free to correct me if I'm wrong somewhere :-)
5th Oct 2019, 1:42 PM
$hardul B
$hardul B - avatar
0
this might help:- mystring = 'bunny' print(mystring[0]) print(mystring[1]) print(mystring[2]) print(mystring[3]) print(mystring[4]) mystring[0] = 'f' # <- you can't do this with a string, it's immutable
2nd Oct 2019, 6:58 PM
rodwynnejones
rodwynnejones - avatar
0
rodwynnejones yes. That why s1=s1.UpperCase() need to give error? because original value changing?
2nd Oct 2019, 7:40 PM
Jayakrishna 🇮🇳
0
Seb TheS so is there no difference between strings and integers in according to immutability?
2nd Oct 2019, 7:48 PM
Jayakrishna 🇮🇳