In Java, String is an immutable predefined class that stores character sequences and cannot be modified after creation, requiring a new object for any changes, while StringBuffer is a mutable predefined class that allows modifications to the same object without creating new instances, making it more efficient for frequent modifications and thread-safe due to synchronized methods.
Inmersión profunda
Prerrequisito
- No hay datos disponibles.
Próximos pasos
- No hay datos disponibles.
Inmersión profunda
String vs StringBuffer l javalearninghub_4_ICSCAñadido:
Hello everyone.
Welcome back to Java learning hub for ICSE.
I hope you all are good today.
And our today's topic is string versus string buffer.
So, we are going to see the difference between the string and string buffer including the definitions, example, and syntax and few interview questions. It is not only necessary or important for the interviews, it is also important for the students who are going to learn.
So, let's see about the string and string buffer functions.
Before we begin, I request you all to like, share, and subscribe my channel Java learning hub for ICSE as so that more videos can be uploaded.
Do like and watch the video till the end. Now, again, let's see what is string in Java.
It is nothing but it is a predefined class because you see if you see here we're writing S with capital here.
So, it is a predefined class in Java which is used to store a sequence of characters.
Say, a set of words together is a character set of is a sequence of characters.
And it is an immutable.
What does immutable means? The things which cannot be changed once it has been assigned or once it has given a value.
That is called as immutable. You can see that here. A proper definition is also is there. An object whose values cannot be modified after creation is called immutable object. So, once we gave like say for an example, if I say that I have given a value A as five.
Then say it is in your object. It is my object. So, it is it will be five throughout. I cannot change it.
Right?
This is what it means.
So, instead of A, you can write over here as well.
That depends on the thing.
See here, example is also given.
String is immutable. Why it is immutable? Because once we are going to create the object of that particular string, its values cannot be changed.
I hope you all are understanding this.
If you face any problem, please give me the comments.
And um I will try to rectify your doubts.
Now, let us see about the string syntax.
What we will do, we will write a string followed by the variable name equals to, that is an assignment operator, and we write a string.
Or whatever the value is there, that value you'll write in double quotations.
And then, as usual, you have to terminate it with semicolon. Right? Now, let us see an example of it.
What do we have here? We are having a variable S as string data type, whose value is Java.
What we are trying to do here is in this particular example, we are trying to append or attach two values.
Other one is S is Java, as we know. We are appending or joining with uh other word called as programming.
So, that when we display the value with the help of system.out.println, your output will be What will be your output? Your output will be Java programming.
Right? Your output is Java programming.
I hope you are following me.
Now, let us see about the string buffer.
String buffer is also a predefined.
Predefined means we have different names, basic, fundamental, or already existing, or already defined when the language is written.
Right? So, it is also a predefined class, which is used to store and modify character sequences.
Sequences. Now, string before is what?
Immutable.
It is mutable, whereas string string was immutable. So, mutable means changeable.
You can once give the value, again you can edit the value.
So, let us see a definition here.
Object whose values can be modified after creation means modifications can be done.
Then, we will call such objects as mutable.
Initially, I gave it as five, then I'm making it as 50. That is possible.
We saw We say that string buffer is mutable because the content can be changed without creating a new object. For the existing object only, we can modify the changes.
Now, let us see an example of string buffer syntax. Sorry, it is a syntax.
String buffer is class name. SB is an object equals to new string buffer, and we will pass the value here.
It is similar to that of your scanner object creation. Here, what we are doing? We are taking it as Java, but in scanner object, we write system.in.
Right? So, this SB is an object whose value is Java here.
Now, see here the example.
String buffer SB is equals to new string buffer Java. So, Java is is going to be saved in SB variable. Now, we do the same thing where we have used plus or concat method we use in strings, here we are going to use a method called as append.
It is one of the string buffer method.
With programming. So, whenever we are going to print system.out.println, SB value will be Java programming. Same as that of the previous one, but with different methods, right?
Got that?
Now, let us see some differences between string and string buffer.
As first difference, we all know that it is string is immutable, no changes allowed.
It whereas string buffer is mutable, changes can be done on the in the object value.
And string is bit slower.
Whereas string buffer is faster because it came after the string.
Whereas string, for doing the same, doing any changes, we need to create new object every time we use it. Whereas in string buffer, same object can be modified.
I hope you all are following. Please like, share, and subscribe the channel.
Now, before we end this, let us see a few examples or sample questions for which usually you get for the interview. What are those?
Here, main difference between string and string buffer, you can give the answer as string is immutable, where value cannot be changed once created. Whereas string buffer is mutable, means value can be changed after creation.
Then the next question, which class is thread safe?
String or string buffer?
So, we have to give the answer as string buffer is thread safe because its methods are synchronized.
String is not thread safe.
Then which one is more efficient for frequent modification? String or string buffer?
I personally think that it's a string buffer, right?
And string will be a non-efficient.
Because they modify the same object without creating the new object, man, changes are required.
Can we change the content of string? No.
Strings cannot be Values cannot be changed, whereas string buffer can be changed.
Now, difference between string buffer and string builder.
So, both are mutable and similar. Both can have the changes, but string buffer is synchronized or in hand in hand, let's say, whereas string builder is not synchronized and it is a bit faster.
I hope you all have understood about this.
And I request you all to go through this. If you have any comments or requests, please give the comments and like, share, and subscribe my channel.
And your help is needed for further videos. Thank you.
Videos Relacionados
Agentforce NOW AMA: Build with React and Salesforce Multi-Framework
SalesforceDevs
490 views•2026-05-28
How agent o11y differs from traditional o11y — Phil Hetzel, Braintrust
aiDotEngineer
450 views•2026-05-28
Re: 🗣️📍theprophedu📍2026 GST 103 CLASS (E-EXAM REVISION)
theprophedu
636 views•2026-06-04
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation💯✅
LearnwithSahera
1K views•2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 views•2026-05-29
Search Algorithms Explained in 60 Seconds! 🤖💨
samarthtuliofficial
218 views•2026-06-01
People of Game of Thrones using JavaScript DOM
AltCampus
296 views•2026-05-30
Instagram accounts got PWNed
EricParker
13K views•2026-06-03











