I will present an article about different string Classes in java and comparison between them.
There are three String Classes in Java
The most common class used in Java programming is java.lang.String class.
To identify which Class we should use it, it depends on requirement, So let's first make comparison between them and at final get conclusion about guidance use.
1- Mutable or ImmutableImmutable objects are simply objects whose state (the object's data) cannot change after construction like String, Integer, ......, Mutable objects are the opposite.
String is immutable object, that means if we have two String objects (str1, str2) , then we need third object(str3) to concatenate str1 and str2
String str1 = "Mahmoud"; String str2 = "A. El-Sayed"; String str3 = str1 + str2;
StringBuilder and StringBuffer are mutable objects, that means that we don't need another object to Concatenate extra characters.
StringBuilder strBldr = new StringBuilder("Mahmoud"); strBldr.append("A. El-Sayed"); StringBuffer strBfr = new StringBuffer("Mahmoud"); strBfr.append("A. El-Sayed");
2- Memory ManagementIn previous point when we use String to concatenate strings, we create third object to handle concatenation value, but with StringBuilder and StringBuffer we don't need another object to do concatenation.
StringBuilder and StringBuffer is best in memory management specially when handling big string data.
3- equals() MethodThe String class overrides the default equals() method, but StringBuffer and StringBuilder do not override the default equals() method.
String str1 = "Mahmoud"; String str2 = "Mahmoud"; StringBuilder strBldr1 = new StringBuilder("Mahmoud"); StringBuilder strBldr2 = new StringBuilder("Mahmoud"); StringBuffer strBfr1 = new StringBuffer("Mahmoud"); StringBuffer strBfr2 = new StringBuffer("Mahmoud"); System.out.println("str1.equals(str2) =" + str1.equals(str2)); System.out.println("strBldr1.equals(strBldr2) =" + strBldr1.equals(strBldr2)); System.out.println("strBfr1.equals(strBfr2) =" + strBfr1.equals(strBfr2));
The output of the example in java console
str1.equals(str2) = true strBldr1.equals(strBldr2) = false strBfr1.equals(strBfr2) = false
4- StringBuffer and StringBuilder methodsStringBuffer and StringBuilder have the same methods with one difference in synchronization.
StringBuffer is synchronized (which means it is thread safe and you can use it when you implement threads for your methods) but StringBuilder is not synchronized (which implies it isn’t thread safe).
5- StringBuilder and StringBuffer PerformanceStringBuilder is faster than StringBuffer.
So you should use StringBuilder class unless if thread safety is necessary, you must use StringBuffer class.
6- ConcatenationYou can concatenate string using "+" operator, but in StringBuilder and StringBuffer you can't,
You can concatenate string in StringBuilder and StringBuffer using append() method.
ConclusionAfter state most difference between different string Classes you should follow the below points
1- Use String class when the object doesn't change as String class is immutable.
2- Use StringBuilder class when object value will change and it will be accessed from single thread as StringBuilder is unsynchronized
3- Use StringBuffer when object value will change and it will ve accessed from multiple threads as StringBuffer is synchronized.
Mahmoud A. El-Sayed