- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the difference between String, StringBuffer and StringBuilder classes in Java explain briefly?
The String class of the java.lang package represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. Strings are constant, their values cannot be changed after they are created.
The StringBuffer and StringBuilder classes are used when there is a necessity to make a lot of modifications to Strings of characters.
Unlike Strings, objects of type StringBuffer and String builder can be modified over and over again without leaving behind a lot of new unused objects.
The StringBuilder class was introduced as of Java 5 and the main difference between the StringBuffer and StringBuilder is that StringBuilder’s methods are not thread safe (not synchronized).
It is recommended to use StringBuilder whenever possible because it is faster than StringBuffer. However, if the thread safety is necessary, the best option is StringBuffer objects.
Example
public class Sample { public static void main(String args[]){ String str = new String("Hello how are you"); StringBuffer sb = new StringBuffer(str); String str2 = sb.reverse().toString(); System.out.println(str2); } }
Output
uoy era woh olleH
- Related Articles
- What is the difference between StringBuffer and StringBuilder in java?
- What is the difference between a StringBuffer and StringBuilder in java?
- Difference between StringBuilder and StringBuffer in Java
- Difference between StringBuffer and StringBuilder.
- Java String, StringBuffer and StringBuilder Tutorial.
- Difference between string and StringBuffer in Java.
- Difference Between String and StringBuffer Class in Java
- What is the difference between a String object and a StringBuffer object in java?
- Explain the equals() method of the Object, String and, StringBuffer classes.
- Difference between String class and StringBuffer class in Java
- Difference between String and StringBuffer.
- Where to use a StringBuffer/StringBuilder than a String in Java?
- Difference between String and StringBuilder in C#
- What is the difference between static classes and non-static inner classes in Java?
- Explain briefly the difference between Mechanics, Kinematics, and Dynamics.
