Difference between String class and String Buffer/ String Builder class in Java

0
15

 

This tutorial is all about Difference between String class and String Buffer/ String Builder class in Java. In this post we will learn , difference between String class and String Buffer/ String Builder class in Java.

  1. String objects are immutable while String Buffer objects are mutable.
    String temp1=” Rahul”;
    String temp1=”Rahul”+”Tiwari” ;
    System.out.println(temp1);
    Here, output will be Rahul Tiwari. But two objects will be created.
  2. In String class, toString, equals and hashcode methods are overridden while in String Buffer only toString method is overridden.
  3. String objects can be sorted because String class implements comparable interface while String Buffer objects cant be sorted.
  4. We can create String object without using new operator but we have to use new operator for creation of String Buffer/ String Builder objects.
    String temp1=”Rahul”;
    No need of new operator. A new object is created.
  5. String and String Builder class is non synchronized while String Buffer class is synchronized.

 

Facebook Comments
(Visited 12 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here