Site icon TechVidvan

Difference Between String and StringBuffer in Java

String in Java

A string is an object representing a number of character values for Java. The Java string object is made up of a separate character value for each letter in the string. The char class represents the characters in Java. An array of char values can be written by users, which means the same thing as a string.

Example : String s="dataflair";

Stringbuffer in Java

A sequence of characters that are thread safe, mutable. The buffer is similar to a string, but it can be modified. Whenever there’s a particular sequence of characters, the length and content can be adjusted through some method calls. The use of string buffers by more than one thread is safe.

Example :

class StringBuffer
                         {  
                           public static void main(String args[])
                               {  
                                  StringBuffer sb=new StringBuffer("Hello ");  
                                  sb.append("Java");
                                  System.out.println(sb);                       
                               }  
                        }

Uses of String

Uses of Stringbuffer

Difference between String and StringBuffer in Java

SNO     String         Stringbuffer
  1     The String class is immutable.     The StringBuffer class is mutable.
  2     String is slow and consumes more memory when we concatenate too many strings because every time it creates a new instance.       StringBuffer is fast and consumes less memory when we concatenate the strings.
  3     String class overrides the equals() method of Object class. So you can compare the contents of two strings by the equals() method.     The StringBuffer class doesn’t override the equals() method of an Object class.
 4     String class is slower while performing concatenation operation.     The StringBuffer class is faster while performing concatenation operations.
  5     String class uses String constant pool.     StringBuffer uses Heap memory

Advantages of String

Text processing:

Data Representation:

Ease of Use:

Compatibility:

Disadvantages of String

Memory consumption:

Immutability:

Security vulnerabilities:

Advantages of Stringbuffer

Mutable:

Efficiency:

Threadsafe:

Disadvantages of Stringbuffer

Program on String and Stringbuffer

import java.util.*;
class TechVidvan
{  
public static void main(String args[])
{  
StringBuffer sb=new StringBuffer();  
System.out.println(sb.capacity()); 
sb.append("Hello");  
System.out.println(sb.capacity());
sb.append("java is my favourite language");  
System.out.println(sb.capacity()); 
}  
}

Output
16
16
34

String vs StringBuffer

Since String is immutable in Java, whenever we do String manipulation like concatenation, substring, etc., it generates a new String and discards the older String for garbage collection. These are heavy operations and generate a lot of garbage in the heap. So Java has provided StringBuffer and StringBuilder classes that should be used for String manipulation. StringBuffer and StringBuilder are mutable objects in Java. They provide append(), insert(), delete(), and substring() methods for String manipulation.

Conclusion

The StringBuffer class is faster than the String class and provides a number of additional methods for deleting the sequence elements, updating the sequence elements, etc. In the heap section of memory, an object called StringBuffer is allocated.

Exit mobile version