Difference between String and StringBuilder in java

In java, String, and StringBuilder class both are classes that used to create a string in java. Both classes provide different ways to create a string that we have learned in the String class and StringBuilder class.Let’s find all the difference between String and StringBuffer in java.

Difference between String and StringBuilder in java
  String StringBuilder
1.   When we create a string by String class, The JVM creates an immutable string in heap memory. We can’t modify the string in the same string object, if we try to modify it the JVM creates a new object of String and update the string value to a new object. You can read why string is immutable in java.When we create an object of StringBuilder, The JVM creates a mutable string in heap memory. We can modify the string in the same string without the creation of a new object. You can read the mutable string in java.
2. If we talk about the performance, the performance of String class is slower than StringBuilder. Let’s see the example of performance comparison.It is perform operation more quick as compare to the String class. The performance is faster than String. Let’s see the example of performance comparison.
3.   The object of the String class takes more memory in heap memory because of immutable string. It creates a new string after each modification in a string object that leads to more consumption of memory.The object of StringBuilder class takes less memory than the string class because of mutable string. When we made any changes in string the JVM doesn’t create a new object.
4.  To make a comparison of two strings we can use the equals() method of String class. The string class overrides the equals() method. The equals() method returns boolean value. To make comparison of two string of StringBuilder class we need to convert them in object of string class. We can convert them by use of toString() method. After conversion, we can use the equal() method.


5.  
When we create the object of String class, the JVM initializes the length of the string. We can’t change the length of the string object after creation. StringBuilder class always creates a growable object because we can modify it after creation. We can reinitialize the same object with new length of string.
6.   The object of the String class always stored 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()
    {  
        StringBuilder objectOfStringBuilder = new StringBuilder("Ravi");  
        for (int i = 0; i < 10000; i++)
        {  
        	objectOfStringBuilder.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 StringBuilder = "+ 
        (System.currentTimeMillis()-startTime)+"ms");  
    }  
}  

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

Leave a Comment