Subject: In this post we will learn , difference between String class and String Buffer/ String Builder class in Java.
- String objects are immutable while String Buffer objects are mutable.
String temp1=” Rahul”;
String temp1=”Rahul”+”Tiwari” ;
Here, output will be Rahul Tiwari. But two objects will be created.
- In String class, toString, equals and hashcode methods are overridden while in String Buffer only toString method is overridden.
- String objects can be sorted because String class implements comparable interface while String Buffer objects cant be sorted.
- We can create String object without using new operator but we have to use new operator for creation of String Buffer/ String Builder objects.
No need of new operator. A new object is created.
- String and String Builder class is non synchronized while String Buffer class is synchronized.