Difference between String and StringBuffer

In the last post we discussed the difference between StringBuffer and StringBuilder. Here we are gonna discuss the differences between String and StringBuffer class.

String vs StringBuffer

1)Mutability: String is immutable (Once created, cannot be modified) while StringBuffer is mutable (can be modified).

Example –String is immutable:
String str = "Hello World"; str = "Hi World!";

By seeing this code you would say that the value of str has changed so how can it be immutable? Let me explain this:

In first statement an object is created using string literal “Hello World”, in second statement when we assigned the new string literal “Hi World!” to str, the object itself didn’t change instead a new object got created in memory using string literal “Hi World!” and the reference to it is assigned to str. So basically both the objects “Hello World” and “Hi World!” exists in memory having different references(locations).

tringBuffer is mutable:Lets see StringBuffer mutability
StringBuffer sb = new StringBuffer("Hello"); sb.append(" World");

In the first statement StringBuffer object got created using string literal “Hello” and in second statement the value of the object got changed to “Hello World” from “Hello”. Unlike Strings here the object got modified instead of creating the new object.

2) Performance: While performing concatenations you should prefer StringBuffer over String because it is faster. The reason is: When you concatenate strings using String, you are actually creating new object every time since String is immutable.

What java documentation says about StringBuffer:

From StringBuffer javadoc:

A thread-safe, mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.

There are many differences between String and StringBuffer. A list of differences between String and StringBuffer are given below:

No.StringStringBuffer
1)String class is immutable.StringBuffer class is mutable.
2)String is slow and consumes more memory when you concat too many strings because every time it creates new instance.StringBuffer is fast and consumes less memory when you cancat strings.
3)String class overrides the equals() method of Object class. So you can compare the contents of two strings by equals() method.StringBuffer class doesn't override the equals() method of Object class.
Performance Test of String and StringBuffer
import java.io.*; public class Test { public static String concatWithString() { String t = "Java"; for (int i=0; i<10000; i++) { t = t + "Tpoint"; } return t; } public static String concatWithStringBuffer() { StringBuffer sb = new StringBuffer("Java"); for (int i=0; i<10000; i++){ sb.append("Tpoint"); } return sb.toString(); } public static void main(String[] args) { long startTime = System.currentTimeMillis(); concatWithString(); System.out.println("Time taken by Concating with String: "+(System.currentTimeMillis()-startTime)+"ms"); startTime = System.currentTimeMillis(); concatWithStringBuffer(); System.out.println("Time taken by Concating with StringBuffer: "+(System.currentTimeMillis()-startTime)+"ms"); } }


Output:
Time taken by Concating with String: 3349ms Time taken by Concating with StringBuffer: 1ms
String and StringBuffer HashCode Test

As you can see in the program given below, String returns new hashcode value when you concat string but StringBuffer returns same.

import java.io.*; public class Test { public static void main(String args[]) { System.out.println("Hashcode test of String:"); String str="java"; System.out.println(str.hashCode()); str=str+"tpoint"; System.out.println(str.hashCode()); System.out.println("Hashcode test of StringBuffer:"); StringBuffer sb=new StringBuffer("java"); System.out.println(sb.hashCode()); sb.append("tpoint"); System.out.println(sb.hashCode()); } }


Output:
Hashcode test of String: 3254818 229541438 Hashcode test of StringBuffer: 4072869 4072869



Instagram