Difference between String and StringBuffer in java

In java, String and StringBuffer both are classes that used to create a string in java. Both classes have different ways to create a string that we have learned in the String class and StringBuffer class. In this post, we will see the difference between String and StringBuffer in java.

difference between string and stringbuffer
  String StringBuffer
1.   String class is used to create an immutable string. It means we can’t change/modify it. If we modify it the string, JVM creates a new object of string. You can read why string is immutable in java.It is used to create a mutable string. It means we can change/modify it and JVM doesn’t create a new object of string. You can read the mutable string in java.
2. The String class is slower than StringBuffer. The performance of the String class is slow than StringBuffer. Let’s see the example of performance comparison.It is faster than the String class. The performance is faster than String. Let’s see the example of performance comparison.
3.   String consumes more memory in heap memory because when a user modifies any string it creates a new object of string. That degrades our performance and increase memory.StringBuffer consumes less memory than the string class because when a user modifies any object of StringBuffer it doesn’t create a new object that improves the performance also.
4.  We can compare two strings by use of equals() method because the string class overrides the equals() method. The equals() method returns boolean value. StringBuffer class doesn’t override the equals method. If we want to compare the string we need to convert it in String class by use of the toString() method. After conversion, we use the equal() method.


5.  
The object of the String class is of fixed length. The length of the string it initialized at the time of string creation. The object of the StringBuffer class is growable. When we modify the string the length of object automatically grows.
6.   The object of String can be store in String constant pool or heap memory. The object of StringBuffer is stored in heap memory.
class PerformanceExample
{  
    static void concatinationWithString()  
    {  
        String objectOfString = "Ravi";  
        for (int i = 0; i < 10000; i++)
        {  
        	objectOfString = objectOfString + "kant";  
        }  
         
    }  
    static void concatinationWithStringBuffer()
    {  
        StringBuffer objectOfStringBuffer = new StringBuffer("Ravi");  
        for (int i = 0; i < 10000; i++)
        {  
            objectOfStringBuffer.append("kant");  
        }  
          
    }  
    public static void main(String[] args)
    {  
        long startTime = System.currentTimeMillis();  
        concatinationWithString();  
        System.out.println("Time taken by String object = "+ 
        (System.currentTimeMillis()-startTime)+"ms");  
        
        startTime = System.currentTimeMillis();  
        concatinationWithStringBuffer();  
        System.out.println("Time taken by StringBuffer = "+ 
        (System.currentTimeMillis()-startTime)+"ms");  
    }  
}  

Output: Time taken by String object = 142ms Time taken by StringBuffer = 2ms

Leave a Comment