17 June, 2012

Strings in Java


I will present an article about different string Classes in java and comparison between them.
There are three String Classes in Java

1- java.lang.String
2- java.lang.StringBuilder
3- java.lang.StringBuffer

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 Immutable
Immutable 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
Example
     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 Management
In 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() Method
The String class overrides the default equals() method, but StringBuffer and StringBuilder do not override the default equals() method.
Example
     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 methods
StringBuffer 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 Performance
StringBuilder is faster than StringBuffer.
So you should use StringBuilder class unless if thread safety is necessary, you must use StringBuffer class.

6- Concatenation 
You can concatenate string using "+" operator, but in StringBuilder and StringBuffer you can't,
You can concatenate string in StringBuilder and StringBuffer using append() method.

Conclusion
After 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.

Thanks
Mahmoud A. El-Sayed

2 comments:

  1. Nice article. By the way even if you use "+" for concatenation it internally replaced by StringBuffer or StrinBuilder based upon JDK version, see here.

    ReplyDelete

How to Pass Parameters to ActionListener in ADF

In some cases, it is required to pass a value to ActionListener of ADF Button. The method that can be invoked by actionListeners has only...