In Java, String class creates immutable strings where equals() compares content, while StringBuffer and StringBuilder create mutable strings where equals() compares memory addresses; StringBuffer is synchronized (thread-safe) while StringBuilder is not, making StringBuffer suitable for multi-threaded environments and StringBuilder preferred for single-threaded performance-critical applications.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
Diffrence Between String,String Builder,String Buffer Day 39 By KishanAdded:
yes yes yes very very good evening guys very very good evening all right so let us start our session here today so guys before starting our session let me tell you Saturday there is no class I mean unfortunately like my health is not good on Saturday I'm unable to take the class so I hope it's okay for you kindly please don't mind without any prior information I have fill the class please don't mind guys please don't mind okay and yeah right now I'm okay right now I'm fine right now I'm fine but on Saturday what happened you know like once my office is completed when I came to the home uh completely I I got like exhausted like uh I understand that I may not be able to give you my best explanation for you so something like disease and thing of happened on Saturday so what I did you know Saturday immediately I confirmed to my our team in our naresh it like I'm unable to take the session please inform to my students and immediately I had milk warm milk and I just slept that's it Sunday morning uh almost like 7 30 8 o'clock I woke up so after that I'm fine but uh I'm sorry guys I'm sorry like Saturday I'm unable to take the class please don't mind please don't mind okay fine fine it's okay it's okay it's okay no issues no issues all right guys thank you guys thank you for uh understanding and again please uh I'm sorry for canceling the class without any prior information please don't mind okay all right so okay so let us start our session here today so guys before starting our session what are the topics we actually which we have covered I believe we have foreign I believe we have completed regarding yes we have concentrated on strings concept we have uh completed strings and after that we had seen okay this what is the importance of SCP and heat memory also and then I believe we have seen The Logical programmings on strings also did we covered logical programmings guys did we covered logical programmings on strings yes yes yes we have covered logical programmings on Friday I guess right on Friday entire Friday we discussed only about logical programming substrings yes yes that is great that is great all right guys right right now guys uh in our very first starting days of strings like uh on Wednesday when I started discussing about strings concept I told you we can create strings by using like string string buffer and string Builder right string string buffer and string Builder now it is the time for us to understand what is the difference between string string buffer and string Builder strings like when we are speaking about strings yes of course we can create strings by using string class we can create strings by using string buffer class and we can create strings by using string Builder class asymplastics now guys believe me when I say this this is one of the most commonly asked interview Point application one of the most commonly asked interview point of equation you will get to this question what is the difference between what is the difference between string string buffer string string buffer and string Builder one of the most commonly asked into your point of view question okay so we'll be able to will be able to answer this question once the completion of this session okay now now so the very first point which you need to remember here is the very first point which you need to remember in strings in strings so guys strings which were created by using Stringer class I repeat strings which were created by using string class are immutable immutable strings you already know this am I right guys strings which were created by using string class are immutable immutable Okay so strings which were created by using string buffer are mutable strings which were created by using string buffer are mutable in the same manner strings which were created by using string Builder also mutable guys by the way by the way what is this string string buffer and string Builder what are these string string buffering string Builder those are come on exactly classes those are classes string is a class Mukesh these are classes you need to say like that string is a class string a buffer of course it is a class it is a class and Stringer Builder also it is a class so what I want to say is if we are trying to create strings by using string class those are immutable if you are trying to create strings by using string a buffer those are mutable string Builder also mutable mutable now now dot equals method e q u a l s dot equals method in string class dot equals method in string class it will dot equals method in string class what it will check guys you already know this what it will check c o n t e n t s contents dot equals method in string class it is going to check the contents okay the same dot equals method the same dot equals method in string buffer class in string a buffer class it is going to compare the it is going to compare the address locations the same dot equals method it is going to compare the address locations let me try to do one thing just let me remove this I will be trying to write like this that would be better so this is I mean dot equals method it will compare the address locations now here dot equals method it will compare the content let me remove this like this now dot equals method in string class it is going to compare the contents the same dot equals method the same dot equals method in string buffer class it is going to compare the address locations address locations and in string Builder class also it will check the address locations as simple as this asymptustis okay guys again again I'm saying you you will get this question in the interviews you will you will get this question so please concentrate correctly okay now now in string class strings which were created by using string class are immutable and Dot equals method will check the contents present inside the string string buffer strings which were created by using string buffer are mutable and Dot equals method will checks the address locations address locations and when you are speaking about string Builder same same exactly same but sir what is the difference between what is the difference between string buffer and string Builder wait for it guys can you please wait for it I'll let you know I will let you know hello NS locations just a second address locations like this address locations okay I will let you know guys I'll let you know can you please wait for it now let us try to work on a small program guys okay let us try to work on a small program not just it is my ID okay now let me try to take a method display method now inside the display method I'm trying to create some strings okay string s equal to new string new string and here I'll be going with Java j a V8 Java like this now string I mean let me try to create string buffer new string buffer new string buffer and here let me try to create Java Java like this okay equal to b u f f here effer buffer it's an identifier guys it's just an identifier and string buffer like this this is string class string class this is string buffer class this is string buffer class okay now let me try to create string Builder also okay new string Builder and here I'll be going with JV Java like this string Builder Java like this now equal to b u i l d r Builder s t r i n g u i l d r string Builder like this okay so right now I have created three strings string Builder class okay now guys are you observing the program can you please respond in the chat are you observing a program right now I have created three strings three strings first one by using string class second one by using string buffer class third one by using string Builder class yes is an identifier buffer is an identifier Builder is also an identifier just a name just a name that's it not more than that I hope you get I mean you are getting me okay now now for example for example guys yes I mean if I want to join two strings if I want to join two strings is there any method present in string class if I want to join two strings is there any method presentation class yes fantastic what is that method concat method yes now I mean yes.com cat yes dot concat I'm going with uh I am going with these LDS for me awesome like this is awesome like this okay now my question is can I write bu f f here buffer.com cat can write buffer.comcat is awesome like this can I write like this please think and do let me know please think uh sir my first question is my first question is is concat Method present in string buffer class the answer for that question is no the answer for that question is no concat method is not present in string buffer class sir if that method is not present in string buffer class how can we call that method with help of string buffer object so we can't are you understanding me guys are you kidding me so we cannot we cannot use concat method if you are dealing with string buffer okay so then what I need to do like I want to join two strings okay guys if you want to join two strings by using string buffer by using string buffer then instead of concat method we are supposed to use append method what is the method name what is the method name append append okay now the same method will be present in Builder also Builder class string Builder also dot append and here I am going with ease awsome is awesome is awesome like this okay so guys concat is a method which is present in string class to join two strings append is a method which is present in string a buffer class to join two strings append is again the same method will be present in string Builder class also which is used to join two strings am I clear with this am I clear with this now guys here you need to understand one point here you need to understand one point that is point is every method every method present in every method in every method present in string a buffer is also present in string Builder every method present in string buffer class every method every method present in string buffer class exactly the same method is present in string Builder class same exactly the same method so then you are saying that you are saying that string buffer mutable dot equals method will compare the address location string Builder mutable dot equals methods comparison address location now again you are seeing that you are saying that same methods are present in string buffer and string builder then what is the difference between string buffer and string Builder we are not getting it I understand wait for it wait for it guys can you please wait for it okay now so remember this point remember this point every method present in stream buffer class is also present in string Builder class okay now now system dot out dot printerland system dot out Dot println and here from string class I am trying to print string class data plus yes after that [Music] after that now here I am trying to go with string buffer class data bu f f here string buffer class data b u f f e r I am trying to print a buffer and here I am trying to go with string Builder class data and here I am going to built app builder okay Now what is my expected output here guys what is my expected output data very good wrong madhu yes good good very good Mukesh wrong wrong Krishna good good Krishna Naveen Chetan Suma Muhammad Manoj yes that is good that is good that is good right right okay okay okay all right guys all right guys here we will be getting our output as Java we'll be getting our output as Java why you will be getting our output as Java sir strings which were created by using string class are immutable immutable sir okay that is good that is great what about string buffer string buffer and string Builder these are mutable m-u-t-a-b-l-e these are mutable are you understanding me guys so here you will be getting your output as Java is awesome and here also we'll be getting your output as Java is awesome like that let me go to my main method please now inside my main method let me try to create an object new class EA and please have a reference aov J belongs to class A like that now a obj sorry okay uh right now a o b j dot a obj dot display method I am calling my display method now let us check our output right click run as Java creation are you understanding the concept here guys please do let me know are you getting me now generally what will be happening here you know what will be happening you know for example for example when you are speaking about immutability when you are speaking about immutability let us think that you are having a heap memory okay let us think that this is Heap memory this is heat primary okay now when you are creating strings like this just a second foreign no when you are creating a strings like this let me try to yes I hope it's visible is it visible is it visible please tell me yes yes that is great that is great so just uh like this now what will be happening here you know first three objects will be created three string objects will be created like what Java jav Java just um now Java will be pointed I mean Java that will be having an that will be having an address location let us think that address location okay I will give you I'll give you the address location also just a second let me try to create three objects okay one and three like this just a second just a second Java like this one more object let me try to create one more object like this two objects got created let us think that this is having some address location as seven for one this is having some address location as 752 and this is having some address location has seven eight nine like this now now yes will be pointed towards this 789 buffer will be pointed towards this 7 4 1.
buffer Builder will be pointed towards this 752 like this guys I hope it's okay like are you getting me are you kidding me yes yes yes that is great that is great it will be pointed towards like this okay now now whenever we are using concat method if you are using concat method then what will be happening here you know like you already know that actually what happens if you are using that concat method a new odd come on tell me guys what happens if you are using concat method a new object will be created means this is the code this is the code if you are trying to write our code like this copy okay now let me try to write the code like this okay now this is the code right right now if you are trying to write the code like this then what will be happening you know for here this statement I am speaking about this one for this statement entirely a new object will be created am I right or wrong entirely completely a new object will be created means let us think that let us think that entirely a new object something like Java Java is everybody is going to be awesome entirely a new object is created that's it and it is not having any reference by the way a new object is created and let us think like it is having some uh some for some I mean just some number address location that's it there is no reference for this literally there is no reference for this now when you are speaking about buffer dot append what is the value of buffer guys what is the value of buffer buffer buffer value is Java am I right guys you need to help me out please help me out buffer value is Java okay for this string Java you are adding another string that is is AWS will be awesome whatever the changes which you want to do you are performing those changes for the existing object only here also is every so every awesome like this here also like this now that is the only reason that is the only reason if you are trying to print yes here if you are trying to print s value what is the data you are getting yes yes is pointed towards 789 and you will be getting your output as Java only right but when you are trying to print buffer buffer is pointed towards sound for one and here your output is Java is awesome in the same manner Builder also the output is Java is awesome are you understanding me guys why we are getting an output like this because because strings which were created by using string buffer are mutable mutable guys is this Justified is this Justified is this point Justified did you understand this point fantastic very good very good now now guys please now please observe here please observe here guys in a CB SCP concept is only available for what SCP concept is only available for Strings string class string class for string buffer and Stringer Builder I repeat for string buffer and string Builder there is no concept related to SCP always the data will be present inside the Heap memory only am I clear with this right right okay now let us try to understand what is this dot equals method also okay now guys please observe here string string S2 equal to new string new string and here I am going with the Java new string Java okay now I am trying to take string buffer string buffer buffer 2 equal to Java is going to be awesome like this in the same manner in the same manner string Builder p u i l d are string Builder 2 equal to sorry this is string Builder right string Builder string Builder equal to like this okay like this like this okay so I have created two strings I have created two strings and I have created two string buffers okay let me make this as buffer one Builder one buffer one filter one and here also Enterprise okay guys I hope it's okay for everyone is it okay did you understand the modification I just change the identifier names that's it not more than that okay so so now please observe here please observe here guys system dot out dot println system dot out Dot println string class string class plus ah S1 dot equals S1 dot equals S2 now system dot out dot println sys system dot outdoor printlamp twing buffer class string buffer class string buffer class buffer 1 dot equals buffer 2.
buffer1 dot equals buffer two in the same manner I'll be going with string Builder class Builder class and here I'll be going with Builder 1 dot equals Builder 2.
foreign guys did you understand these three statements did you understand these three statements okay can you please tell me what is my expected output please tell me what is my expected output right exactly exactly guys I will not be getting same how I will be getting same yes one what is the content present in S1 the content present in S1 is Java and what is the content present inside S2 Java guys content present in S1 here what is the content present in S1 Java only right come on come on once again can you please read this please read it please read it equals method equals method in string class what it is going to check what it is going to check in equals magnet string class it is going to check the exactly contents guys content come on guys this is very very important you are very very important so content present in S1 what is the content present in S1 Java what is the content present in S2 Java so true or false true or false true now in buffer in string buffer and string Builder What DOT equals method will check in string a buffer and string Builder dot equals method will checks the address locations address locations now tell me now tell me what is the output here true or false sir 100 false yeah guys 100 why because we are using new keyword right whenever we are using new keyword 100 a new object will be created so here also our output will be false and here also our output will be false right click run as Java application and this is not string class true string buffer class false and string Builder class also forms guys I hope it's clear for everyone is it clear please respond to me guys please respond to me a very very very very very important Point very important difference so spring string buffer string Builder okay now now sir what is the difference between what is the difference between string buffer and string Builder yes yes guys string buffer is string buffer is synchronized string buffer is synchronized string Builder is not foreign buffer is synchronized and Stringer Builder is not synchronized sir what do you mean by synchronization what do you mean by synchronization guys synchronization is a concept which we will be studying whenever we are dealing with multi-threading multi-threading but as of now let me try to give you a very very small example for synchronization for example for example right how to say this for example okay guys I believe most of you people are having brothers and sisters like yes most other people are having brothers and sisters right okay you just imagine one small scenario your father bought a box of chocolates your father or mother bought guys if you are having brothers and it's just fine like or else cousin brothers and cousin sisters will be there now right so just imagine just imagine a small scenario your father bought a group of chocolates right he kept that between you and your brother or else your sister right he told to take them nicely to share equally is it going to happen it's going to happen right right right so guys here okay uh some people are saying uh sometimes and sometimes not some people are saying most of the people are saying no 100 no exactly so here imagine that box of chocolates is a resource is a resource okay now yourself is one thread your brother or your sister is one thread okay thread thread the name itself is thread how we are having a name as Constructor likewise in our Java we are having a concept known as thread now how many threads are there tell me how many threads are there right now like you know in this example how many threads are there two threads are there I mean yourself is one thread either your brother or sister is wanted right so two threads are there now these two threads are trying to act on a resource am I right or wrong resource means box of chocolates at the same time at the same time what will happen a small war will happen I don't know about you but in my home definitely it's going to happen right at that time what will happen like mother will be coming and she will try to divide the team members the right or wrong so basically what you want to say here is what I want to say is guys if two threads are trying to act on a resource at the same time how we will be getting inconsistent results yeah it may be a box of chocolates or else it may be a TV remote or else it may be a mobile phone it may be car keys or else by case you can imagine in your own situations guys are you getting me right yes so you will be getting inconsistent results in order to avoid that type of situation in our Java we are having a concept known as synchronization synchronization if we provide synchronization then what will be happening you know one thread should act on that resource and the second thread should wait until the first thread has completed its execution no chance no way no way are you kidding me guys are you understanding me for example for example if me and my sister is there we are just let us think that we are fighting for car keys fighting for carthis then my mother will come she will say that first let sister go what I'm supposed to do I don't have any other option I should wait I should wait am I right or wrong until her trick piece completed literally I can't do anything that is synchronization the same process we can achieve in Threads also so how to achieve that synchronization we'll be discussing in our whenever we are dealing with multi-threading am I clear with this am I clear with this now tell me one thing I said string buffer is synchronized now tell me can multiple threads Act same time in swing buffer exactly I'm impressed guys very good very good very good guys synchronize it means multiple threads cannot act on the same time right they should wait they should wait when you're coming to non-synchronized remains non-synchronization means multiple threads can act on the same at a time so we will be getting yeah simply you can think like non-synchronization means simply or yes that is correct that is correct I hope you're understanding me guys are you kidding me right so regarding this synchronization and non-synchronization we will be trying to discuss whenever we are dealing with multi threading did you got it now now this is an entire point of equation what is the difference between strings which were created by using string class string buffer class and string Builder class yes if you are trying to create a string by using string class if you are trying to create a string by using string class those are immutable and Dot equals method will compare the contents if you are trying to create strings by using string buffer class those are mutable and Dot equals method will compare the what what what not equals method will compare address locations address locations when you are speaking with string Builder those are also mutable and Dot equals method will compare the address location then what is the difference between string buffer and string Builder search string buffer is synchronized string Builder is not synchronized sir then which one I am supposed to take which one I am supposed to use string class or string buffer class or string Builder class guys basing upon your requirements for example for example in your project if you are trying to modify string many times multiple modifications are there in your project then don't go to string you need to create by using string buffer or string Builder are you understanding me guys multiple modifications are there means every time a new object will be created if you are dealing with strings that is not recommended so if multiple modifications are there in your project we are supposed to go to either string buffer or string buildup either string buffer or string Builder if you want to like use synchronization concept then go to string buffer or else you can go with string Builder this is the difference between strings which were created by using string class string buffer class n string Builder class one of the most important interview point of equation okay is it clear guys is it clear no no guys please observe this observe this one more method void show method void show method now let me try to create just let me try to create entirely new buffer okay new tring buffer new string buffer I'm just trying to create string buffer like this equal to SB belongs to stri in the string buffer like this now guys first string buffer and string a builder there will be a default capacity default capacity I am not speaking about the length I am speaking about the default capacity the default capacity of string buffer is plus SB dot capacity is 16.
the default capacity of string buffer is 16x16 I'll show you I'll show you please observe this let me try to call aov J dot show method now please observe here right click run as Java application what is the default capacity 16 16 in that sense 16 in the sense string buffer can by default accept 16 characters sir after 16 characters it will not accept no no it will accept it will accept the size will automatically increase automatically the size will be increased for example please check this please check this um SP dot append SP dot append let me just try to use a b c d e f g h i j k l m n o 16 completed I think 15 completed uh 16 completed 16 completed now I have added 16 characters I have added 16 characters after adding 16 characters I am trying to print the capacity SB dot capacity okay now my output is 16.
my output is 60 just let me try to print SP SB Plus okay capacity guys capacity not length not length what is the length what is the length length right now right now SB dot okay just let me try to write like this SP sorry system.out Dot println s b dot length just let me try to print Le and GTH length plus like this okay right now the length is 16.
I'm right around 16 16 okay now now let me try to add let me try to add one more character a Pender SB dot SB dot append and I am trying to go with q I'm trying to go with Q okay now if I am trying to print the length can you please tell me what is my length here here my length is 16. now here what is my length what is my length there come on go ahead go ahead go ahead I'm seeing your outputs please please exact yeah that is 100 correct guys that is 100 correct that is 100 correct we will be getting we will be getting length as length as you can check right click run as Java application 17. sir what is the capacity what would be the capacity so guys after adding 16 characters after adding 16 characters then the size of the the size of the buffer will increase basing upon this formula can you please tell me can you please tell me how much foreign right click okay let me try to get the capacity let me try to get the capacity copy paste like this and here you are saying 34 let us check is it correct is it correct after adding 32 I mean 34 again the same thing current capacity Plus One into two likewise the capacity of the string buffer will be increased guys the default capacity of string buffer is 16.
default capacity of string buffer is 16 the size will increase basing upon our data what are the data which you are entering basing upon the data the size will be increasing it okay right right right now guys just like just like string class we are having some methods which are present in string buffer also can you use these methods I mean we already use a length method we already use capacity method can you say carrot method what carrot method will be doing okay system dot out dot println system dot out dot println carrot method carrot method just for example for example methods methods ah tell me Plus SB Dot SP daughter carrot five tell me what is tell me what is my output here index position always starts with zero 0 1 2 3 4 5 guys don't forget that don't forget that you are please that is basic you should not forget that you should not forget that okay for example if we are using reverse if you are using reverse method then what will happen r e v e r s c reverse method reverse matter Plus s b dot reverse directly we are having a method that is reverse method reverse method it will completely reverse the string entire string will be reversed I hope it's clear guys now can you please do me a favor can you use these methods can you use them yes yes or no that is good that is good guys please do me a favor please use these methods and uh if you are having any doubts please feel free to ask me go ahead first try this program please write this program please write this program uh Krishna array size is completely different Krishna don't go with array size okay how to compare data types of two objects what do you mean by that question I did not understand the question yes of course same concept will be there for string Builder exactly same dollar string buffer and string Builder are exactly same the only thing is that is not I mean string buffer is synchronized string Builder is not synchronous that's it okay gaura right uh Puja for that you don't have a specific method okay you need to write the logic you need to write the logic yeah you don't have a specific method for that we need to write our own logic guys if you understand string class correctly then obviously string buffer and string Builder easily you'll be able to understand that I hope you understand string class correctly did you that is great foreign [Music] foreign oh Ravi I'm good thank you thanks for asking foreign foreign foreign okay bhubaneswar eshwar in line number 40 number 40 the capacity once we have exceeded the default capacity when you are trying to add one more character you will not be getting any error orals and exception what will be happening you know the capacity the default capacity of the string buffer which is 16 will be increased to 34.
the size will automatically increase after reaching the default capacity okay yeah if we add one more character also the size will be 34 only the size will be 34 only foreign language is enough one is more than enough okay Abhijit yes exactly exactly Durga did you understand Emma Forex supporting should be there really for example if you are going with Java supporting means SQL HTML those are supporting Technologies if you are having those supporting Technologies also then that would be like added advantage in your resume yeah we can store our best no problem foreign foreign please increase completed Krishna and that is hexa integer code of that object okay that will be printed as internal implementation of two string method is it I hope it's completed guys is it done all right all right so guys I hope it's complete for everyone okay please note on the output also please note on the output also I mean if you want you can not download tutorials you can take a screenshot output no problem did you remember when we discussed about object class I told you object class object class did you remember that two string method get class method equals method finalize method all these methods are present in object class buddy you did not remember at least one as one method also come on right so guys again I'm trying to say regarding this 34 how we are getting this 34 we are supposed to use this formula current capacity plus 1 into 2.
16 plus 1 current capacity is 16 16 plus 1 17 into 2 34. after adding 34 elements if you are trying to go more than 34 elements then 34 plus 1 into 2.
like that okay cross out right right
Related Videos
VALORANT's Latest 'Exclusive' Tier Bundle is Rough...
KangaValorant
17K viewsβ’2026-05-28
Flight Attendant Mocks Poor Looking Black Woman β Mid Air Announcement Exposes Her Real Power
SkyboundStories-b4r
184 viewsβ’2026-05-28
I FIXED My Friendβs Blown Turbo RX-8β¦ Then Sold It
Cameron-RX8
134 viewsβ’2026-05-28
NewsWatch 12 at 5: Top Stories
NewsWatch12
1K viewsβ’2026-05-28
Simon Jordan & Danny Murphy deliver PREDICTIONS for Arsenal's Champions League FINAL with PSG
talkSPORTArsenal
6K viewsβ’2026-05-28
Botting is OUT OF CONTROL in Classic WoW (Again)...
SolheimGaming
108 viewsβ’2026-05-28
The "AI Job Apocalypse" is CANCELLED!
WesRoth
9K viewsβ’2026-05-28
STREET FIGHTER 6 - INGRID Story Walkthrough @ 4K 60αΆ α΅Λ’ β
RajmanGamingHD
12K viewsβ’2026-05-28











