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

Which of the following is not an isolation level in the JDBC

1872


Is linked list a linear or non-linear data structure?

730


Explain different ways of creating a thread. Which one would you prefer and why?

788


Does java trim remove newline?

720


Is null keyword in java?

713


How to restrict a member of a class from inheriting by its sub classes?

1059


what are different ways in which a thread can enter the waiting state? : Java thread

697


Why do we declare a class static?

742


Which collection is sorted in java?

779


What is command line used for?

814


Explain the importance of import keyword in java?

768


Can variables be used in java without initialization?

776


What is math floor in java?

663


What is passed by reference and pass by value ?

755


Difference between method overloading and overriding.

768