Why string is immutable in java || The 4 reasons you must know || part 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey everybody welcome back so in this series of tutorial we are going to cover why string is immutable in Java now you must be thinking why I'm saying a series of tutorial because there are differently since there are different factors which max changing mutable and immutable behavior of stream is helping us in many ways while we are creating a real-time application so we are going to explore everything about it in this delays of tutorial but in this particular video we are going to talk about we're gonna learn a little about already now what we understand about immutable and the mutable behavior of a object and also we are going to cover one of the reason why string is immutable so string is really a very important chapter in Java because you know in your real name application you use string pretty much and also it is very important in interview point of view so let's get started with our talk [Music] all right so now let's understand what we mean by the immutable classes or immutable objects all right so to play I have a class here okay I'm just putting a plastic on it all right and let's say my class has two properties okay two variables one is int I it will depend and another one is in J equal to 2 right so I have two different variables you okay I am Jake so let's say I'm treating the object of my class let's think like I have a main method here and I'm creating the object for my class a a equal to new a alright so much gonna happen in your memory a offset is going to be created all right and this a is going to your audit so whenever I take is getting created this cop is all in our static variables so now I need you found a stain and say well you will come in soon all right so now what we understand about mutable and immutable thing all right so this is the stand above that so if I do like this by 8:30 and 8:45 do this thing what is going to happen it's obviously going to change this variable Bailey right so these are properties isn't it these are my properties of the subject so my object is in a state right now this is in this this is in a state right now I'm changing the state of the subject right now this variable is I believe a list of the p30 and the change a variable is going to be putting my object stay is getting changed right my object or any difference take previously right now I just changed the state of it of the subject right so this is called Newton right this is called a mutable class right i a classes new table that's why I'm able to the properties of a objectors are a stain of the subject so immutable means you can change the state of announcement all right but even make this class computer alright how to make immutable I will have a dedicated video for that I think probably in the last part of this tutorial series will have a video about that but this is not you miss this is an immutable class is anything beautiful class and if this is a suitable class I am NOT allowed to change the properties of this object I'm not allowed to change the state of the substance so in an immutable classes the object state cannot be changed think that this is an irritable class and think I'm feeding you of that a if you want to knew it all right now an object is getting created any sentencing pays attention to this particular object I have I equal to 10 and j.t right now once of this is getting created that means I cannot change the properties of this I am J once this object is created means I cannot change the stable disc object and this is what we mean by immutable objects the immutable adjustments the object state cannot be changed we cannot change the state of this object right so this is the difference between mutable and immutable means I can change the state of an object immutable means once the object is getting created I cannot change the state of the properties simple right all right so now let's come back to our topic string is immutable in Java so what do you mean by that so the Sun Microsystems people the developers who have created stream they made string plus immutable that means once you create an object of the string you cannot change the object you cannot modify that object or you cannot change the container properties of the dogs so what I mean by that this thing like I have a string object there's a string STR equal to of heaps so I have this dream object okay so what happens in the nanny a string of them is getting created forget about string constant would he stop for a moment okay just take like inside your memory a string object is getting created with the content of the AVI and this STR is referencing to this particular object all right so if I try to modify the subject right now this is not possible I cannot try to modify this object this object property you should not be changed this object content should not be changed or this object state cannot be changed so unity of all means we cannot change the state of an object right so string is immutable that's why we cannot change the state of the subject so I will be trying to do like this Str dot-com cat okay it's the STR concat of laughs so collage is my name so things like not trying to come carry and trying to modify if I want to try to add something with a subject this is not going to affect this object all right because once the object is getting created means we cannot change this properly so you have tried to change the properties of the subject for example trying to $45 bodies of this object then what will happen and new object will be created in the memory with the modified content so see I'm trying to forget lash with this particular object right so you can end up earning from here okay and it will are everything large I enter in your set so you for trying to modify something with this object there is going to create a new object with the modifier yet right so anyhow there is no damage has been done with this particular object this of the state is not getting changed so now we have 3d system powered the println of SPR what is going to give it to me so it's very simple right now Str is referencing to this particular object right and this particular object contained is puffy so I am going to get obvious enough so how good will be right so okay if I'm trying to modify any object is getting created but the thing will move this over here right now this object does not have a reference so it doesn't have a reference it is the editable part canvas collection all right so try to bring this thing if I try to see this thing then I can keep the hair it's like stream is equal to h2 plus now this s is beautiful through this particular object now we must bring SOP of s now this s is right now going to give me this as an out of a lush all right so this is going to do all right so now let's understand why string is immutable in Java and to make you understand this I am going to bring string constant equal to the picture because twinkle Standford plays a really vital role for our memory optimization so let's understand about that a bit so let's talk about I have a string here okay so stream HDR if you re do um alright so whenever I write this what happens internally next about this is your memory area and you have your heel this is your eternia and this is your spring constant pool all right so whenever I am creating a string object here this is a B will go lose three in constant Y because I'm reading it by or I'm creating it by using literals these are spring laters right this is a constant if this is the first and this will go to the stranger stand cool so I absolutely create an insider Springboks can put with a fun thing of me all right and this SDR is going to read for you this particular object code of I am creating another stranger string str1 equal to V again now what is going to be done all right so I'm reading it by using double Gore so this is the literal or a constant so if it is the constant it should go through Street concern but wait it is go ahead and create it is going to check either an object is already available with the same content and yes that is already inside the strength of Stanford with with this content of e so this is not going to create a another object inside HDB instead it is going to network to the object this is already created inside the stream or Stanford so we say whenever when creating a object it is pretty of the cover here and catch it with a stream of Stanford so whenever we're creating another object it is just referencing to this particular is already in there right because doing it is not allowed inside screen from Stanford okay so this is called else passage because whenever is getting created is this cash inside the string constant pool and after that if we are creating any objects with the same content it's just want to add water to this particular object which is already there inside the screen inside hood instead of creating another one I'm creating another string over here string str2 and again given the stem content of the game so anything will happen again is the literal you should go to the screen because a constant we're already up there is over here with the same content of e so there is no chance of creating another of the top units at the spring comes tranquil because two tickets are not allowed in stream I mean the biggest are not allow inside the sprinkles chemical so this SDR one is going to pointing to the same object which is already been cashed inside the stream from Stanford so can you see how string constant food is helping us for memory optimization because we don't need to create multiple objects of racket think for every alright all this if I have a Pascal student okay I am creating the application for student or something okay and the classical student and I have picked out this today with the same name in India so it is going to cast it and whenever we are creating any of them with the same content it's just a quarter this to the object which is already there inside the spring comes trapper very simple all right so now let's think for a minute okay let's say I'm changing this entity or 200 I do STR to.com cat and I will run cat last year okay so last does be something else in Hindi so let's not think about it so this sta-21 cap a flash right so what does it happen is it going to now this STR to this reference input this object right so if I am going to the comic at something it is not going to the subject is already there inside the string constant pull instead it is going to create it create a new object inside every failure because I am doing some primetime operation operation you need created inside you keep it so and you will be created with the modified rendering of a large ok so it is not affecting the object over here why because string is immutable say for every if stream is beautiful thing like that if your string is mutable and if I'm trying to modify something up here with this SD guide to object to this out there if you can modify this object now think like for a moment this is mutable and his teens this object is a change in the state of this object right so now what is going to happen now this object gotta change because of this is tau now str and str one is also referencing to the same object did you see a veneer str and SPR one is also referencing to the same objects inside the spring constant hood so if stream is mutable all this all this object is already referencing to this particular object inside the stream first and put their menu is also getting changed now their venom is a big right now there is also going to change your glass because there they are definitely to the same object and this is changing the property of this object over here so all these objects are getting affected things like each other student inside this particular application or inside this particular student aah and all their resonance is also getting finished because this is not food right so that's why string is immutable I mean this is one of the reason why string is immutable so this is what we need to remember all around there's a big cash with us inside the string constant pool so because of spins implementation the string is mutable because we just cannot modify the object which is cash inside the spirit constrain pool because other references and also in a pointy toes pointing to this particular objects right so this is the one of the reason why string is immutable so this is it for this video in the next video we are going to talk about you know how the mutability or strain is helping us to make string as a key in the hash map and what are the many bits we are getting because of the immutable behavior of string object alright we're going to talk about that but before I wrap up this video I just cannot stop myself to explain you one more thing because I you know I explained about the passing inside the spring constant pool so one of the another person you might also get in your interview is that okay so let me clear this things up see here this way I have a string object string is all right so I just stick it over here so what happens this is your memory and this is your restraint constant pool area so this is the literal so it will be created inside the string and pull and this edge is going to network with this particular object and the content of this object will be okay right so now let's assume that I am fitting a another stream okay spring s 1 equal to a new string off of EA all right so this is an another way of getting string by it so whenever I am creating a string in usually the new Q R so I'm doing an operation so the opposite is going to be created inside your heel right which is 10 20 right and there will be no object will be played it's a straight cut straight put it was already available with the same content okay so now another so there we know there are created it's at the spring contractor right so we're not all right so now the portion gonna be now this s1 will be pointing to this nugget right so I don't want that I want to use the object which is already there at the string from Stanford so how can I do that so I want to use the object inside the hip area I want to use the object which direct cast inside the string constant so how can I do that so for doing that I will use a metric called internal method okay the method name is internal right so now this object is not interned right so to make any turn I just understand that and in terms right so this internal matter is a variable inside your string API so if I'm measuring it in turn now what is what is going to be happening now this s1 right now is dropping the point with this particular object memory instead it is going to pointing to the object which is there inside of stream number after a string constant okay this are there will be get used right so right now I should check the reference but if you do system.out.println is equal to equal to s1 so don't you get the stem reference you think going to get as the other is to write all this references SNS one is pointing to the object which is a in Santa string constant so there it goes so you might get this person in your interview a spare all right so this is it for this video in the next video where you talk about that so if you like this video just give me a like if you don't like his videos give me some sensation down below and if you have any doubts you can push it a comment down below or you can write email to me excellent preyus at the redeemer calm or you can keep you posted about it in my Facebook page and you'll get all the links down in the description alright so that's it for today thank you very much for watching I'll see you in the next video [Music] [Music] [Music]
Info
Channel: Selenium Express
Views: 94,843
Rating: 4.8757653 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, string in java with example, java interview questions and answers for experienced, java interview questions and answers, string pool memory management in java, string pool in java, string pool vs heap, string pool concept in java, intern method in java, why string objects are immutable, slenium express, what are immutable objects in java
Id: r9tnet8I8VE
Channel Id: undefined
Length: 20min 53sec (1253 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.