String is very important and most commonly used object in java programming and we should know some important features of String as a java developer. Here we will some of the important features of string and how it is different from String builder and String buffer.
String:
Since String is immutable object and if we perform operation like concat() and assign it to existing string, it will create new string and delete the old string for Garbage Collection.
String string1 = "java";
string1.concat("wleomce");
System.out.println(string1);// it will print javaonly
but if we do below
string1 = string1.concat("language");
System.out.println(string1);// it will print java language and it deletes "java" literal
So if we do these operation on string, will create lot of garbage on heap. Hence Java provided StringBuilder and StringBuffer classes to manipulate the strings.
Both StringBuilder and StringBuffer are mutable objects and we can perform append(), delete(), subString() etc methods on string object.
StringBuilder vs StringBuffer
1. Thread Safety : Both are mutable classes but StringBuilder is synchronized and all method are synchronized. String builder provided thread safety but it effects on performance. Hence java provided new class StringBuffer in JDKversion 5. StringBuffer is similar to String builder but it omits thread safety and it is not synchronized. Hence we can use StringBuilder in single thread environment else use StringBuffer.
2. Performance: Due to synchronization and thread safety, StringBuffer is less efficient on performance when compared to StringBuilder.
package com.sample.java.testing;
public class StringBuilderTest {
public static void main(String[] args) {
String s = "java";
StringBuilder builder = new StringBuilder(s);
builder.append(" is OOP language");
System.out.println(builder.toString());//java is OOP language
}
}
package com.sample.java.testing;
public class StringBufferTest {
public static void main(String[] args) {
String s ="Welcome to java";
StringBuffer buffer = new StringBuffer(s);
buffer.delete(0, 10);
System.out.println(buffer);// java
}
}
Below are some of basic differences in between String , StringBuffer and StringBuilder
String:
- String class is represents as characters ans we can create string object as
- When we create String with double quotes, first it will check whether same string is available in string pool or not and based on it;s existence, it will create new string in pool or return existing reference. This approach will save lot of memory by using same string for multiple threads. But here if we go for new operator to create string object, it will create new memory in heap memory. Every string created with new operator will create memory in heap.
- String is immutable in java and we can share it to different functions , classes and threads safely and String is final class and fields also are final except hashcode .
- Two string are equal only when both strings have same characters in same order. And String equals() method is case sensitive. We can go for equalsIgnoreCase() if we do not want to check for case sensitive.
Since String is immutable object and if we perform operation like concat() and assign it to existing string, it will create new string and delete the old string for Garbage Collection.
String string1 = "java";
string1.concat("wleomce");
System.out.println(string1);// it will print javaonly
but if we do below
string1 = string1.concat("language");
System.out.println(string1);// it will print java language and it deletes "java" literal
So if we do these operation on string, will create lot of garbage on heap. Hence Java provided StringBuilder and StringBuffer classes to manipulate the strings.
Both StringBuilder and StringBuffer are mutable objects and we can perform append(), delete(), subString() etc methods on string object.
StringBuilder vs StringBuffer
1. Thread Safety : Both are mutable classes but StringBuilder is synchronized and all method are synchronized. String builder provided thread safety but it effects on performance. Hence java provided new class StringBuffer in JDKversion 5. StringBuffer is similar to String builder but it omits thread safety and it is not synchronized. Hence we can use StringBuilder in single thread environment else use StringBuffer.
2. Performance: Due to synchronization and thread safety, StringBuffer is less efficient on performance when compared to StringBuilder.
package com.sample.java.testing;
public class StringBuilderTest {
public static void main(String[] args) {
String s = "java";
StringBuilder builder = new StringBuilder(s);
builder.append(" is OOP language");
System.out.println(builder.toString());//java is OOP language
}
}
package com.sample.java.testing;
public class StringBufferTest {
public static void main(String[] args) {
String s ="Welcome to java";
StringBuffer buffer = new StringBuffer(s);
buffer.delete(0, 10);
System.out.println(buffer);// java
}
}
Below are some of basic differences in between String , StringBuffer and StringBuilder
- String is immutable class where as StringBuffer and StringBuilder are mutable classes.
- StringBuffer is thread Safe and StringBuilder is not thread safe
- String Builder class is useful to manipulate String in single thread environment where as StringBuffer will be useful to manipulate String in multi thread environment as it is thread safe.
No comments:
Post a Comment