9.9 String is Immutable

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so welcome back alias is now introduced on political happenings and in this video we'll talk about why string is immutable and how exactly we can benefit from that now they talk about mutability there is one person who has by a lot and he says mutability is a crime and I we all are criminals right we always change a change those data right so when you say mutation it means change right so let's say if you have an object and you want to change the values then you are doing mutation so we need to make sure that we are doing something which will not mutate it so there are certain things if you want to increase the performance of your system make it immutable right so make an object as immutable so logically it will increase the performance so string of the strings are immutable because we want to increase the performance right now let us see how to how to do that so what happens is normally be like this straight way which is string STR equals to let's say if I write my name which is Naveen okay make sure it is I okay people normally search me with nav ee en it's not that is na VI n okay so if any VI ends ready don't use W then now let's say we have this name which is a string STR equal to Naveen now unless we have what so this is a string right so what happens is inside your heap memory we have something called as this small let me let me take a big area so inside your heap memory will be having something called as what it is called as string pool so instead of heat memory will be having something a string pool so now if I say string STR equal to Naveen okay so this will go inside this and there will be a variable there will be a value called as Naveen okay and now this mapping will have a unique address like name it as 1 to the 1 now what will happen if I create a new variable as string str1 equal to the bean okay same the same object now okay so we have the same value but we have gr one so what will happen and this it will always create new object now inside your string pool since we already have Naveen it will not create new object both of this which I put to the same object right so there will be a stack movie right normally if you want what happens is whenever you take an object you use two memories one you use a stack memory right and then you use the heap memory so objects are normally put it in sidekick memory and the reference which is STR and str one is a variables right which will be inside your stack memory and somewhere here you mentioned that this is STR right and you will mention one zero one and since HDL is 1 is also 1 0 1 so now we have two variables which are pointing to the same object now what if I take STR ok this is with the first variable and if I say STR equal to ready if I change this now what will happen will it change the existing name of course not right because if STR is changing to ready if I change this one till ID 1 0 1 that means it will it will also affect STR one right so that's touched not a decade right that's why this will create a new object here and the value of that will be ready and later this is 1 0 3 now 1 0 1 will be replaced by 1 0 3 that means you are changing this value of the reference but you are not changing the actual value and that is what makes strings as immutable you cannot change the value okay so once you have defined the string you cannot change it so this is the object which are not changing you are just creating your object so every time you change the value of STR you are actually changing the address but you are not you're actually creating the object but you're not changing the existing value ok and that's why strings are immutable but then question arise we have one more way of feeding string right we can say string s1 equal to new string and in fact you can pass Naveen anyone you can do this right venule thing when you use new keyword that means it will not use ship memory code it will not use string porn otherwise it will create a new object here and that's the address is 2 0 1 okay so it because you are saying new so new will not take the the memory from string pools it will use the memory of sheep here we are not using any new keyword that's why we are using string pool right so this since we are using new it will use the object here which is the inside people you will be having an object and that object will have Naveen but although that's not the case every time you say Naveed it will actually use the memory of string pool ok - that is if it dependents easier so this moment will be also coming from this Naveen here so of course you are using you are using a string pull memory indirectly okay so this Naveen will be coming from string pool so this STR here will have a memory address which is 1 0 1 so that's the does the problem with all the stuff so even if you are saying I mean that will use a string pulse memory but then question arise how to make it mutable and so can we make string new table yes we have two ways of doing it the one way is using string buffer and the another way is using string builder of course we are we have the practical implementation of those in the industries so this is the PD of wire string is immutable ok it so first it it increases the performance of the system second every time you create a variable because to to reference can share the same object right this is actually called as flyweight pattern so in design patterns we have a concept of flyweight which means if possible we can actually reuse the objects I mean to reference are using the same object so that's about why string is immutable if you have any more questions you can come into the comment section or just ask just upon our announcement we are the website is up and up and running you can actually give tests on websites on my websites now which is the disco calm you can also download the Android app which is there on the Play Store now you can search for go to play store and search for periscope app and downloaded try to I mean use it there might be some bug reports news view if you find any bugs where you can resolve it and that's the type of watching and do subscribe for for the video
Info
Channel: Telusko
Views: 228,841
Rating: 4.7996964 out of 5
Keywords: navin, reddy, tutorial, java, strings, introduction to strings, strings in java, java strings, immutability, immutable, new operator, string pool, thread safe, what is, how to, use, objects, String, concept of immutability, creating string objects., telusko, code, program, string
Id: to9DPVsdByE
Channel Id: undefined
Length: 6min 48sec (408 seconds)
Published: Wed May 25 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.