Aptitude (12) ASP.NET (2) Automata (4) Browser (1) C (5) C# (1) C++ (10) Code (3) CSS (1) Data Structure (1) DATABASE (3) HTML (1) java (43) JSP (1) math (1) MySql (8) other (6) php (3) Servlet (3)

Sunday, 28 April 2013

String vs StringBuffer vs StringBuilder in Java

String vs StringBuffer vs StringBuilder in Java
Difference between String, Stringbuffer and StringBuilder
String is one of the most important classes in Java and anyone who
starts with Java programming uses String to print something on
console by using famous System.out.println() statements.
Many Java beginners not aware that String is immutable and final in
Java and every modification in String result creates a new String
object. So How do you manipulate String in Java without creating
String garbage? StringBuilder and StringBuffer is answer
of this question. StringBuffer is old class but StringBuilder
is newly added in Java 5 along with major improvements like Enum,
Generics, varargs methods and Autoboxing in Java. No matter which
kind of application you are working you will find heavy usage of Java
String class but if you do profiling of your application you will find that
String is the one class which creates lots of garbage because of many temporary String created in program. In this Java
tutorial we will see What is String in Java, some important properties of String in Java, What is StringBuffer in Java ,
When to use StringBuffer in Java , StringBuilder in Java and how it can be used in place of StringBuffer, What
are differences between String and StringBuffer and StringBuilder in Java which is a frequently asked core Java
question and mostly String vs StringBuilder vs StringBuffer. Now let's start with String.
Differences between String, StringBuffer and StringBuilder in Java
String in Java
Before looking difference between String and StringBuffer or StringBuilder let’s see some fundamental
properties of String Class in Java
1) String is immutable in Java: String is by design immutable in Java you can check this post for
reason. Immutability offers lot of benefit to the String class e.g. his hashcode value can be
cached which makes it a faster hashmap key and one of the reason why String is a popular key in
HashMap. Because String is final it can be safely shared between multiple threads without any
extra synchronization.
2)when we represent string in double quotes like "abcd" they are referred as String literal and String literals
are created in String pools. When you compare two String literals using equality operator "==" it returns true
because they are actually same instance of String. Anyway comparing object with equality operator is bad
practice in Java and you should always use equals method to check equality.
3) "+" operator is overloaded for String and used to concatenated two string. Internally "+" operation is
implemented using either StringBuffer or StringBuilder.
4) Strings are backed up by character Array and represented in UTF-16 format. By the way this behavior can
cause memory leak in String because same character array is shared between source String and SubString
which can prevent source String from being garbage collected. See How SubString works in Java for more
details.
5) String class overrides equals() and hashcode() method and two Strings are considered to be equal if
they contain exactly same character in same order and in same case. If you want ignore case comparison of
two strings consider using equalsIgnoreCase() method. See how to correctly override equals method in
Java to learn more about best practices on equals method. Another worth noting point is that equals method
must be consistent with compareTo() method for String because SortedSet and SortedMap e.g. TreeMap
uses compareTo method to compare String in Java.
7) toString() method provides String representation of any object and its declared in Object class and its
recommended for other class to implement this and provide String representation.
8) String is represented using UTF-16 format in Java.
9) In Java you can create String from char array, byte array, another string, from StringBuffer or from
StringBuilder. Java String class provides constructor for all of these.
Problem with String in Java
One of its biggest strength Immutability is also biggest problem of Java String if not used correctly. many a
times we create a String and then perform a lot of operation on them e.g. converting string into uppercase,
lowercase , getting substring out of it , concatenating with other string
etc. Since String is an immutable class every time a new String is
created and older one is discarded which creates lots of temporary
garbage in heap. If String are created using String literal they remain in
String pool. To resolve this problem Java provides us two Classes
StringBuffer and StringBuilder. String Buffer is an older class but
StringBuilder is relatively new and added in JDK 5.
Differences between String and StringBuffer in Java
Main difference between String and StringBuffer is String is immutable
while StringBuffer is mutable means you can modify a StringBuffer
object once you created it without creating any new object. This mutable property makes StringBuffer an
ideal choice for dealing with Strings in Java. You can convert a StringBuffer into String by its
toString() method. String vs StringBuffer or what is difference between StringBuffer and String is one of
the popular Java interview questions for either phone interview or first round. Now days they also include
StringBuilder and ask String vs StringBuffer vs StringBuilder. So be preparing for that. In the next
section we will see difference between StringBuffer and StringBuilder in Java.
Difference between StringBuilder and StringBuffer in Java
StringBuffer is very good with mutable String but it has one disadvantage all its public methods are synchronized which
makes it thread-safe but same time slow. In JDK 5 they provided similar class called StringBuilder in Java which is a copy of
StringBuffer but without synchronization. Try to use StringBuilder whenever possible it performs better in most of cases
than StringBuffer class. You can also use "+" for concatenating two string because "+" operation is internal implemented
using either StringBuffer or StringBuilder in Java. If you see StringBuilder vs StringBuffer you will find that they are
exactly similar and all API methods applicable to StringBuffer are also applicable to StringBuilder in Java. On the other hand
String vs StringBuffer is completely different and there API is also completely different, same is true for StringBuilder vs
String.

No comments:

Post a Comment