This is why string is immutable in Java || String Hashcode caching internal || part 2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey everybody welcome back so in this series of tutorial we're covering why string is immutable in Java so in the last video we have covered that what we understand about a immutable class and also we have covered why string is immutable instinct whole point of view but as I said you before there are different reason why the string plus is design is immutable class so today we're going to explore and another reason and the reason is the string used as a key in the hash map pretty much so if you look into your real time application you can find that you are using string as a key pretty much in your house now right so string is a very good candidate in the key in the last map so why is that what is the reason behind it so to make you understand this I might talk about this particular topic string has good carry so whenever you compute the hash code for your string this has been cast in your string object forever right so the US code for the string has been computed for only one time then getting cast inside your string object so what I mean by that okay and how do you miss doing all these things and what is the spring Husker method is all about I mean we're going to talk about it everything practically debug it we will precede this thing internally in this particularly good all right so a lot of the things in the store so let's go ahead and exploring but right after this intro so we move into eclipse let's set up some basic understanding about this as for Cassie right so the string Haskell is only calculated for only one time in your application right so what I mean by that so let's talk about if you have a stream stream is it going to okay so listen is this extreme so here memory there is an object and this is referencing a particular object right so whenever I ask a to calculate the a sport for this particular object what is going to happen here Jen is going to use all these characters to calculate the husband so internally Dylan is going to take in the and ie it is going to power all these characters and this version to calculate the final task for you let's say 1 1 1 2 2 1 1 for an example right your hospital would be a pitcher right so once they live in a place of a sport this has four is going to be cast into your offset so in inside an object this password is going to disturb one one what do you on Monday this will be cast inside your string object so further in my application if there is a need of calculating the class code again JDM is not going to calculate it instead it is going to use this particular task for which this bill already calculated why because thing is immutable right if string is immutable distant it will not be changed right so again with you using the container over here to calculating the faster bright rhythm is looping over all the characters of the spring and calculating the hospital so this is the time to Jimmy process right think about a large stream think about the string with hundred characters like think about actress right having done renamed distinct name state and zip code on off so this is going to be lasting right if we need to talk we need to loop over each of the character in the stream then only we can encounter it in the hospital here so there is no point this doesn't make sense to calculate the hospital again and again because this is a moveable this time it is not going to change so we got rid the hospital for only one time and then casting it inside inside the string upset so internally string is using a variable called has to store you know to store the passport right we're gonna sleep everything practically right now so let's go to eclipse and let's debug things and let's see how the screen has scored method is working in real time all right so let's go operate alright guys so now let's understand how the string casting works okay how string casas his high score and what is happening internally and what chord has been written inside your string has score method and how the things are happening behind the scene alright so to make you understand this I've created a simple class here called string high score testing and I have a main method ok so right now let me go ahead and let me type on a to line up Corps string STR you'll do let's say hello so this hello is the string and I'm going to STR at high score so this piece of line is going to calculate the high score alright alright so right now if I want to see the internal implementation I need to debug this application and so I'm putting a breakpoint over here and I'm just doing a right click debug adds job application and I do have some error in my project so that's not a problem well there you go so that's your full debug screen but to view that I'm gonna minimize it ok so right now I can see my full debug screen over here okay no worries so right now what I'm going to do I'm going to do a step up err okay so I'm in my line number seven so I'm going to hit f6 or this one in the screen right over here I'm going to hit this step over and here we can see a string object has been created and it has the which is zero and if you talk about the value the value is hello right and this is how your string is storing your content hello right so as you know your string is backed by an care array or whenever you store something as a string object the string uses the character array to represent the East characters here so internally string uses and care array to store your elements right so it's e ll although our index the starts from zero and it's goes up to 4 because this hello has the length of 5 ok so 0 to 4 is a 5 length all right so that's about our value but the one that we have interested over here is this hash so this has is 0 because right now whenever we create a string object the has is not computed whenever we request for the high score at that time only it is going to get computer so right now whenever the string object is getting created that has is 0 as you can see over here so right now I want to you know execute this line of code St either high score so right now let's execute this line or or less go into this particular method has called what it is doing and how it is computing a high score let's observe that all right so I'll do a step in to because I want to go into this high score method so as you can see right now I am inside my high score method so let me zoom it a bit so that we can see it properly all right ok so right now we are in our high score matter so so this is what the the developer the sun microsystems people has been written this is our source code and this is our high score method for our string okay so the first thing that you can see over here int h equal to has so this line is actually working as a cache ok or you can say this line is actually cast a high score ok so how we are going to explore it in a bit so first thing that i want to you know get acquainted with so you can see this one this has variable okay so this has is a variable so initially you can see the value of has is zero because it's not get initialized already so right now if I if I open the Declaration over here so we can see our string class is here our string class is public and final and inside your string class we have a variable and the variable name is hash okay and the default value of this has is zero and this has is used to cast the high score okay catch the high score for the string so you can see the comment over here all right okay so now let's understand how this variable has works okay and how is cast a high score okay so so to make you understand this so let's go back to our high score method alright so here we go okay so let's forget about this for a moment okay we are going to get back to this particular point so right now this particular variable H is initialized to has the value of has is zero initially so right now there is a kind of if Clause is over here okay so we can see over here there is a if Clause if you see over here this 8 is equal to equal to 0 this condition satisfied because this has value is 0 already and the length of the value should be greater than 0 so if you talk about the value right now our value is hello right so we can see it over here our value is hello and this has been stored inside a care array so right now this particular condition matches the value of H is 0 because this has is 0 and the value length is greater than 0 because our value is hello and this length is greater than 0 so right now let me do a step over so this comes to this line I'll do a step over it will go into the loop because the condition satisfies and now there is a for loop over here again so this work of this for loop is actually it is going to calculate the high score and how the high score is getting calculated and this being calculated by looping over each character of your string and calculating the high score of it ok so as you can see over here this value of this is 0 because if you talk about this care array you know the first index at the cannery is 0 so it's gonna iterate over each element starting from the 0th index till the last index all right so if I'll do a step over you can see this is going to calculate the high score for each character and once it is done with that it is going to store this edge okay whatever is calculated over here it is going to store that with this hash variable okay so we have seen that this has is actually our variable which is inside the string class and this has variable is used for casting okay so we are going to see that in a bit okay so let me minimize it once again and let me go back to the high score matter alright so there we go alright so if I'll do a step over right now so you can see our has has been calculated and my hash value is double 9 1 6 2 3 double to something this is just say integer all right and right now this edge is going to return me the high score okay because our high score is returning an integer right so we're going to you know return this edge over here but before that whatever the high score is getting calculated over here we are setting this high score to the variable over here has so now the has has been set to this particular value and the value of has is changed to this value from 0 all right so right now if I'll do a step over here I'm going to get back the high score okay so my STR high score right now instead of 0 I'm getting this particular value over here and if I open this I can see the high score has been calculated right but the thing is right now whenever we demand the hash code for our string is been calculated and cashed in the memory I mean it's been cast inside the string object so from now on if any point of time we need the high score calculation again it is not going to calculate e 2 once again it's just simply going to use the cast which is the house variable in this case it's just going to get the value from that particular variable all right it is not going to calculate it once again it is not going to calculate it a number of times because this is a time-consuming process because if think about a large string for a last string the whole loop execution will be done for a number of times right so this is gonna consume a lot of time and for performance wise this is really bad so once the string high score has been computed it's going to be cast in the memory okay so to to you know prove this I'm going to take this high score matter few times more and let's see what is going to happen internally all right so right now if I save it at least is just saying me to restart it so I'll do a restart and let's see what is going to you know happy number here okay so right now let's read on this program once again so if I'll do a step over my string of letter is getting created the house is zero so if I right now I'm over here first time I need to calculate the high score so if I'll do a step into you can see first of all this house is zero and right now if I'll do step over the condition matches our string is hello as I said you already the value is hello so the value is so the value length is greater than zero and the house is equal to zero because an initially the value of house is zero if I will do a step over scores into the loop is calculate the high score by looping over each character as you can see over here and now it's set the high score to this particular has variable okay so if I'll do a step up where I can see this the house variable has been set so if I'll do a step over again it's going to return me the high score as you can see the high score I found over here now the next thing if I'll do a step over again and right now I want to again calculate the high score of the string but remember the string has code we have already computed over here I mean we have already calculated over here in this line so the string casts assist so we don't need to compute it once again so if I'll do a step into I'll goes into the high score method again but this particular loop is not going to be executed because this H value is not zero right now if I see over here this has has been already computed and this particular value has been set to this particular integer H okay threw this into luh so the loop is not going to be perform again so instead if I'll do a step over again it's just going to avoid the loop and come directly to our return statement and it is just going to return me this particular edge and just to just a reminder this H is already hold the value of the Hoss right and so this has is actually as I have already shown you this is actually this particular particular variable and right now the value of has has already been set to this particular value not to zero because the zero value is already been overridden last time right so let me go back alright so I'm here in my Haskell matters so if I'll do a step over so you can see my STR I got the husker for it does not get computed once again is just returning me the cast value so if I were to view that again here again I am calculating the high score so if I'll do a step over here so and if I if I'll go into this particular method again the house value is already been computed and already been stored the hostile you is not zero right now because it's already been computed so this particular loop is not going to be executed once again so if I'll do a step over this it's just going to avoid this loop and directly come to the return statement and if I'll do step over here again I got my high score so the point to prove here is the high score is not getting computed again and again it is a time consuming process because looking over the East character of the string and calculating the high score is a time consuming process so to avoid this we are using string casting because we know the string is immutable and the content is not going to changed so what we are doing over here once the value has been computed the state of the object is not going to be changed so we are casting it in into our string object so in the next time whenever we are requesting our string for the Haskell confucian so at that time what is happening is just giving me back though score which has been already computed and already been cast inside the string object so there's the thing that you need to remember right now let's go ahead alright okay so we understood testing is immutable this why we are not admitting the hospital for the string again and again but how this is helpful all right think about your hash best collections think about it asthma we were using string is it e so here are pretty simple asthma but that in string is a king and integer as a very right my deference is etching and I have used new asthma right to create my husband object so now things like I'm trying to put any object to my map right now and one might even a bear if I'm trying to do any put a present right now even if I'm trying to do any care pressure right now over this map of the nation this passport is going to be calculated the hospital for the key is going to be calculated right so if you have asthma whenever you're doing the frequently you are calculating the hotspot right so I have covered it in my interval implementation of a video right if you haven't watched it yet you can find the link down below in the description but the point of review is you know each time we are calculating the hospital bomb in your mouth eternally is you just created to store any mints right so whenever we're performing a put operation where this is going to be stored in this pocket to find out this the spoon operation eternally click the hospital of this particular key and this is going to place this in Peru bear in a certain pockets right it's going to place it over here the key the menu at the keys a and the value is one and some other information is also a store inside pocket right so to find out in whisper this particular key is right in this 80s in to part of that again the hostel in to be computed for the key a all right so if I bring it here 50 times the hospital be computed operation simply damage the hostel it to be computed for the key right but wait here in this case this is not going to happen why because we're using string as a key and forestry the hostel need to be computed only one time let's say give in this case to input a Christian for the key a the huh Scott is getting computed and once is can compute it it is going to be cast inside the stream object let's say a objecting of tape right so once it's got a cast even if I'm doing typical operation during diskette operation way this get method is going to look for your passport for this a this is already computed so it's just going to be trying that particular a score which is already been cast inside the stream object right so there is no further confusion of the hash code need to be done for this particular gate operation so this is the saving us a lot of time and this is also making the things really fast right so this is why we're using string as a teen the hashmap because string Casas the high score and this is really beneficial for a fashionable driver or assurance or some kind of thing alright guys so let me sum up everything pretty quickly within a minute so you can see I have a class over here called string high score pacing and I'm going to create another class over here called my let's say a mine demo and here in this class I'm gonna override the house court matter so there we go our house court matter has been operating here and I'm just going to have a print statement over here because I'm just going to track it so I'm gonna say a high score matter cult right to sort of make you feel that this house court matter is getting computed every time you are performing you put Orakei to prison all right so right now let's come back to this particular class string husk or pasting and here in this man matter I'm gonna have a hash map and I'm going to take my demo as a key so my demo is my user-defined class and the value I'm gonna take integer I'm gonna give it a friends hm they kill two new hash map now I'm going to create a mine demo object and I'm gonna have a put operation here HM that put and I'm gonna put this M over here this M is the my demo object right and the value I'm gonna give a random value 1 and if right now I'm going to run this application you can see the high score method get called right because during the put operation internally this put matter is calling the husk or method of the key right here my key is my demo M right just belongs to the mine demo class so the husk or method of mine demo class is getting called so right now let's say I'm gonna have a cat operation and I'm gonna look for the key m and you can see my high score method is going to be called two times one for the key one for I mean one for the put operation and one for the cat operation so if I'll do it for few more times so as many time as I'll do the get operation the high score method is going to be get caught so here it is confirmed my put matter and get matter is calling your Husker method again and again because they want to compute the Husker before before inserting an element to the map or when they want to look up for a key inside the map right during the put operation and get a present right but here is the thing that in winter notice I'm here using my demo object as a key right and this particular class is a mutable class right so if you debug it is time it is going to go into the house core method and is going to be compute the hash code right so it's going to be tagged so much time right but if I'm gonna use the string as a key over here right then what's going to be happening here and if I'm gonna insert string I mean string object over here let's say if I'm gonna insert HM dot put and I'm gonna insert a string and value here let's say one right I don't need this so here in this case if I'm gonna have a get operation I'm going to look up for the key AAA so in this case also the husk or Methodist will be get called but the thing is here the high score calculation will be done only for one time right so string kind of works faster then compared to here because this is and that's it at selenium Express and gmail.com so see you in the next video till then bye-bye and happy [Music] [Music]
Info
Channel: Selenium Express
Views: 36,881
Rating: 4.9012346 out of 5
Keywords: string is immutable in java, Why string is immutable in java, string class in java, string in java interview questions, java strings tutorial, hashcode method in java, what is hashcode in java with example, string class hashcode method, string in java with example, java interview questions and answers for experienced, java interview questions and answers, selenium express, abhilash, string class methods in java, java string class, string class methods in java with examples
Id: wytkyBx4JIc
Channel Id: undefined
Length: 25min 58sec (1558 seconds)
Published: Sat Mar 10 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.