-
String, Char, StringBuilder, and StringBufferStaticPL/JAVA 2019. 8. 18. 20:16
1. Overview
Finding Different between String and Char. Check each role on JAVA.
3. Description
3.1 String
It's a Class having a class member of final char[] in it.
It has String Pool in JVM for caching literal reference of instance. but generated separate reference on the pool if instances are created using new keyword each time.
It's immutable like the final instance. So before JDK 1.5, if some concatenation occurred on already existing String instance, new allocation occurred. But from JDK 1.5 these operations using StringBuilder implicitly.
3.2 StringBuilder
Two Classes implements AbstractStringBuilder which doesn't have a final primitive char array, but sole primitive char array. So appending new String is possible in these classes' instances.
It's useful for concatenating Strings. Before JDK 1.5, Every Concatenating using + operator creates new String for storing which is inefficient. But from JDK 1.5, such operations using StringBuilder implicitly.
But StringBuilder is not thread-safe.
3.3 StringBuffer
It's thread-safe because using the synchronized keyword on appended, and so on.
'StaticPL > JAVA' 카테고리의 다른 글
Thread and Runnable (0) 2019.08.18 Stream (0) 2019.08.18 Difference between Abstract Class and Interface (0) 2019.08.18 JAVA Collection Framework (0) 2019.08.18 hashcode and equals (0) 2019.08.18