Friday, September 3, 2010

Java string to use summary

this article is to remind us of the string to be a comprehensive comparison of three types, makes use of three types of strings have a choice based on the time, this procedure can greatly improve the efficiency of operation. If you're already familiar with these three classes, and I assure you, you need to read, if you do not understand what the string, want to have a probably know, ok, continue! If you are to learn from these three types of concrete how to use the, sorry, see the API documentation to ...

The purpose of this article is to remind us of the string to be a comprehensive comparison of three types, making use of string in the three types of time have a selection basis, this can greatly improve the running efficiency. If you're already familiar with these three classes, and I explicitly tell you that you need to read, if you do not understand what the string, want to have about Renshi, ok, continue! If you are the three types of specific learning Zhe how to use the, sorry to see the API documentation. This article made reference to the API documentation, API documentation, but not copy, the public will see!


1, String class

String class instead of the original basic data types, in Java, the string is an object.

String class on behalf of the string. Java programs all the string literal (such as "abc") are implemented as instances of this kind.

String is a constant; their value can not be changed after creation. String buffer to support the string variable. Because String objects are immutable, so you can share them. For example:

String str = "abc";

Equivalent to:

char data [] = ('a', 'b', 'c');

String str = new String (data);

Here are some more examples of how to use the string:

System.out.println ("abc");

String cde = "cde";

System.out.println ("abc" + cde);

String c = "abc". Substring (2,3);

String d = cde.substring (1, 2);

Since it is a string object, then any String variable is not initialized in before it's values are null, in fact, all of the object is not initialized values are null; all the original types of variables in the absence of initialization before the Java compiler will give a default value.

Java language provides the string concatenation symbol ("+") and other objects to the string conversion of special support. String concatenation is through StringBuilder (or StringBuffer) class and its append method implemented. String toString method of conversion is achieved by the method defined by the Object class, and can be Java, all class inheritance. The string concatenation and conversion of more information, see Gosling, Joy, and Steele co-author of "The Java Language Specification".

Unless otherwise noted, otherwise null parameter passed to the type of construction method or methods will throw NullPointerException.

String represents a string of UTF-16 format, which added to the character of the said items from the agent (for details, see the Unicode character Character class representation). Index value is the char code units, so the addition of characters occupy two positions in the String.

String class provides handling Unicode code points (ie characters) and Unicode code units (ie, char value) method.

String to use the trap: String, once initialized, it will not change its content. String string effect on the operation of its copy of the (original copy), in the original string is not changed a bit. For example:

string s = "a"; / / creates a string

s = s + "b"; / / in fact the original "a" string object has been discarded, and now produced a string s + "b" (that is, "ab"). If you repeatedly change the string contents of the implementation of these operations will lead to a large number of copies of the string objects persist in memory, reducing efficiency. If this operation into the cycle, will greatly affect program performance.

On the contrary, StringBuffer class is the original string itself to operate, can be modified without creating a copy of the string copy. Can be used in a loop.

Therefore, if you want to make changes to the string handling operations, it is best to avoid the direct use of String types. Can choose the type StringBuffer.

Second, StringBuffer class

StringBuffer class is thread-safe variable sequence of characters. String a string of similar buffer, but not modify. Although at any point in time it contains a particular character sequence, but can be changed through certain method calls the length of the sequence and content.

String buffer can be safely used in multiple threads. Can be necessary to synchronize these methods, so any particular instance is as if all operations happen in serial order, in that order with each thread involved in the same order of method calls.

The main operations on a StringBuffer append and insert methods, these methods can be overloaded to accept any type of data. Each method can be effective in the given data into a string, then the string of characters appended or inserted into the string buffer. append method always add these characters to the end of the buffer; the insert method to add at the specified point characters.

For example, if z refers to a current content for the "start" of the string buffer object, this method calls z.append ("le") cause the string buffer contains the "startle", whereas z.insert (4, " le ") would change the string buffer, to include" starlet ".

Typically, if an instance of StringBuilder sb reference, then sb.append (x) and sb.insert (sb.length (), x) has the same effect.

When the occurrence of the source sequence of the operations (such as an additional source of sequence or insert operation), the class only in the implementation of this string buffer instead of the source to achieve 同步.

Every string buffer has a certain capacity. As long as the string buffer contains the length of a sequence of characters does not exceed this capacity, they do not allocate a new internal buffer array. If the internal buffer overflow, this volume automatically increases. Starting from JDK 5 for the class to add a single thread using equivalence classes, that is StringBuilder. Compared with the class, usually should be priority StringBuilder class, because it supports all the same operation, but because it does not perform synchronization, so faster.

3, StringBuilder class

StringBuilder class is a variable sequence of characters. Such a StringBuffer compatible with the API, but does not ensure synchronization. Class is designed as a simple StringBuffer replacement, used in the string buffer is used when a single thread (which is very common). If possible, give priority to the use of such proposal, because in most implementations, it is faster than StringBuffer.

StringBuilder, on the main operation is append and insert methods, these methods can be overloaded to accept any type of data. Each method can be effective in the given data into a string, then the string of characters appended or inserted into the string builder. append method always add these characters to the generator end; the insert method to add at the specified point characters.

For example, if z refers to a current content for the "start" of the string builder object, then the method call z.append ("le") would string builder contains the "startle", whereas z.insert (4, "le") would change the string builder, to include "starlet".

Typically, if an instance of StringBuilder sb reference, then sb.append (x) and sb.insert (sb.length (), x) has the same effect. Each string has a certain capacity builder. As long as the string builder contains the character sequence length does not exceed this capacity, they do not allocate new internal buffer. If the internal buffer overflow, this volume automatically increases.

Instance of the StringBuilder is not safe for multiple threads. If you need this synchronization, it is recommended to use StringBuffer.

4, the string comparison

"==" Compare this string to determine whether the same instance, it points to the same memory address.

equals (Object anObject) compare this string is the same instance, it points to the same memory address.

equalsIgnoreCase (String anotherString) this String and another String comparison, without regard to case.

No comments:

Post a Comment