In small programs, we don't realize this because they never face any memory issues, but this type of creating unnecessary objects result in memory and performance issues in enterprise applications.
How do you manipulate String without creating String garbage?JDK provides two more classes for string manipulation. They are : StringBuilder and StringBuffer. StringBuffer is old class but JDK 5 is added a new class i.e. StringBuilder along with other new features and improvements.
While working on a large application, you work with heavy usage of String but if you do profiling of your application, then you will find that String is the one of the class which creates lots of garbage because of many temporary String objects created in program.
- What is String in Java and some of its properties
- What is StringBuffer in Java
- What is StringBuilder in Java and how it can be used in place of StringBuffer
- What are differences between String and StringBuffer and StringBuilder in Java
String class in JavaBelow are some of the fundamental properties of String class in Java:
- String is immutable in Java. Immutability offers lot of benefit to the String class and because of immutability its hashcode value is cached which makes it a faster hashmap key and that's why String is a popular key in HashMap. Because String is final it can be safely shared between multiple threads and doesn't need any synchronization.
- String objects can be created using literal and using new operator. E.g. when we represent string in double quotes like "newTechnoBuzzz", they are referred as String literal and created in String pool. So, when you compare two String literals using equality operator "==" it returns true because they are actually same instance of String.
- "+" operator is overloaded for String and used to concatenat two strings.
- String class overrides equals() and hashcode() method and two Strings are considered to be equal if they contain exactly same character. 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.
- 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.
- String is represented using UTF-16 format in Java.
- 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 StringImmutability which is strength of String class in Java is also its its biggest problem if it is not used properly. Many times, we create a String and perform a lot of operations on it e.g. converting to uppercase/lowercase , getting substring and 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. To resolve this problem, Java provides two classes: StringBuffer and StringBuilder.
Below is the example of String functions:
//To declare a new String object String str1 = "Hello"; //To declare another String object String str2 = "World"; //When we concatenate two string objects then it will create a new object in memory. str2 = str2 + str1;
String vs StringBufferBelow are some of the points:
- String is immutable while StringBuffer is mutable. It means you can modify a StringBuffer object after creating it without creating any new object. This mutable property makes StringBuffer an ideal choice for dealing with Strings in Java.
- StringBuffer can be converted into String by its toString() method.
//To declare a new String object String str1 = "This is a testing string."; //When we concatenate any new value to string then it creates a new object in memory. str1 = str1 + "Add new content in string."; //To declare a StringBuffer object StringBuffer sb = new StringBuffer("StringBuffer example"); sb.append("Append to StringBuffer");
StringBuilder vs StringBufferBelow are the points that makes StringBuilder different from StringBuffer:
- The main disadvantage of StringBuffer is that it is synchronized which makes it thread-safe but same time slow. On the other hand, StringBuilder is not synchronized.
//To declare a new StringBuffer object StringBuffer strBuffer = new StringBuffer("StringBuffer Example"); strBuffer.append("Append to StringBuffer"); //To declare StringBuilder object StringBuilder strBuilder = new StringBuilder("StringBuilder Example"); strBuilder.append("Appending to StringBuilder");
SummaryBelow are list of difference between StringBuffer, String and StringBuilder in Java:
- String is immutable while StringBuffer and StringBuilder is mutable.
- StringBuffer is synchronized while StringBuilder is not. So, StringBuilder is faster than StringBuffer.
- Concatenation operator "+" for String is internally implemented using either StringBuffer or StringBuilder.
- Use String if immutability is required. Use Stringbuffer if you need mutable + thread-safety and use StringBuilder if you require mutable + without thread-safety.