String StringBuffer StringBuilder differences and relations

java.lang.String, java.lang.StringBuffer, java.lang.StringBuilder type is a string, the string is used for processing Java three classes. They are mainly the following differences and contact:

Contact (in common):

Three are final class, not allowed to be inherited.

Due to frequent use, taking into account the performance and safety reasons, to prevent one of the parameters to be modified to affect other application parameters.

Difference :

. 1) String class is immutable class .

String object Once created, the character sequence contained in this object can not be changed until the object is destroyed.

String implements three interfaces: java.io.Serializable, Comparable <String>, CharSequence

2) the StringBuffer class represents a sequence of characters of the variable strings .

The contents of which can be varied StringBuilder append, insert, reserve, setCharAt, setLength like. Once the final string is generated, which is converted toString method call type String.

3) JDK1.5 added a StringBuilder class, and StringBuilder similar , and a method of substantially the same configuration.

The difference is that StringBuffer is thread-safe , may not need additional synchronization for multiple threads; StringBuilder is not thread-safe , running on multiple threads need to use a separate synchronization process, but not as fast as StringBuilder, so performance StringBuilder slightly higher. In the case without considering the security thread, giving priority to the use of StringBuilder.

StringBuilder only implements two interfaces java.io.Serializable, CharSequence, String comparison examples can be compared by compareTo method, the other not.

Three speed: StringBuilder> StringBuffer> String.

Summary :

1) String: Apply a small amount of operation of a character string.

2) StringBuffer: applicable to the case where a large number of single-threaded operating in the character buffer.

3) StringBuilder: applicable to the case where a large number of single-threaded operating in the character buffer.

Guess you like

Origin www.cnblogs.com/sinoaccer/p/12026868.html