1String vs StringBuilder vs StringBuffer
2
3 STRING STRINGBUFFER STRINGBUILDER
4
5Storage String Pool Heap Heap
6Modifiable Immutable Mutable Mutable
7Thread Safe YES YES NO
8Performance FAST VERY SLOW FAST
1The main similarity between String and StringBuffer class is that both are
2thread safe.
3The main difference is that String is immutable; StringBuffer is mutable.
4
5
1The StringBuffer and StringBuilder
2class both produces mutable string objects.
3 The main difference between
4 them is that StringBuffer is thread safe;
5 StringBuilder is not thread safe.
1String vs StringBuilder vs StringBuffer:
2String: Immutable version char sequences
3StringBuilder: mutable version char sequences,
4not synchronized
5StringBuffer: mutable version char sequences,
6synchronized , thread-safe, slow
7String str = new String("a")
8StringBuilder s2 = new StringBuilder("B") // MUTABLE
9StringBuffer s2 = new StringBuffer("c") // Synchronized
1They both mutable, they are exactly same but
2String buffer is thread safe so it runs slower
3than String builder.
4We are using StringBuffer when we are doing parallel
5testing since it is a thread safe.