Difference between String and StringBuffer.

Answer Posted / geetha

String and StringBuilder class stores strings. But when you
cannot change a String object after creating one.
eg: String name = "Prasad";
By saying you cannot change the name object means you cannot
change the value in name object internally. When you change
the name object value to something else, a new String object
is creating in memory and assign the new value.

eg: name = "Prasad Reddy";

A new name object is creating in memory and the value
"Prasad Reddy" is assinging to the newly created space.

But StringBuilder class occupies the same space even if you
change the value.

If you are doing string concatenation StringBuilder class is
far better in performance than String class.

You can use StringBuilder's Append() method to use
concatenation.

Is This Answer Correct ?    5 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How many days will it take to learn java?

523


Why java is considered as platform independent?

597


What is the static import?

599


Is empty in java?

587


Why stringbuffer is faster than string?

551






What's the access scope of protected access specifier?

540


Can you achieve runtime polymorphism by data members?

596


What do you mean by default constructor in java?

582


What is meant by final class?

572


Why char array is favored over string for the storage of passwords?

562


What are memory tables?

612


What is join () in java?

534


What is a flag value?

523


How to make a class or a bean serializable?

547


Is string pool garbage collected?

560