String is no stranger to all Java programmers, and almost every day even every program deals with string, so it's easy to get a common knowledge of string here.
Overview:
The String is defined in Java:
Public final class String extends Object implements Serializable, Comparable<string>, charsequence
String is the final type, continue to see the following explanation:
The String class represents character strings.
All strings literals in Java programs, such as "ABC", is implemented as instances of this class.
Strings is constant; Their values cannot is changed after they is created.
The string object represents a string or sequence of characters, and all literal strings in Java are objects of string;
A String is a constant, and its value cannot be changed once it is created.
String buffers support mutable strings. Because String objects is immutable they can be shared.
Next, the character buffers support variable strings, because the string objects inside the buffer can be shared (in fact, the object's reference has changed).
In Java, a string connector (+) number is provided to convert other types of objects to string types:
The Java language provides special support for the string concatenation operator (+), and for conversion of the other object S to strings. String concatenation is implemented through, the StringBuilder (or StringBuffer) class and its Append method. String conversions is implemented through the method toString, defined by Object and inherited by all classes in Java.
int i = 123;
String str2 = i + "abc"; /** * The above sentence generates the class file as follows: * String str2 = (New StringBuilder (string.valueof (i))). Append ("abc"). toString (); * String to "+" The StringBuilder and its append and toString methods are used. */
Seemingly simple surface, in fact, the operation of complex, rather than directly with the string.valueof to quickly:
int i = 1= string.valueof (1) + "ABC";
Construction Method:
1) Non-parametric construction method
String ()
Initializes a newly created String object so the it represents an empty character sequence.
The constructed method creates an empty sequence of characters, noting the use of this construction method, because creating an unnecessary string object is immutable. It is therefore not recommended to take the following create String object:
New= "Sample";
Such results are conceivable, resulting in unnecessary objects.
2) parameter is a string type
String (string original)
Initializes a newly created String object so that it represents the same sequence of characters as the argument; In other words, the newly created string is a copy of the argument string.
The value of the source string does not affect the value of the target string, but it is used in a relatively small way, perhaps in conjunction with the SubString method:
String original = "1234567890"new String (Original.substring (2, 5));
This is necessary, and when original is a large string, this interception method allows the new string object to point to the new object instead of the original original, reducing memory consumption.
3) parameter is a byte array type
String (byte[] bytes)
Constructs a new String by decoding the specified array of bytes using the platform ' s default CharSet.
String (byte[] bytes, Charset Charset)
Constructs a new String by decoding the specified array of bytes using the specified charset.
Byte is a serialized form of network transport or storage, so the byte[] array and String are required to be converted to each other during many transport and storage processes.
About IO Stream code will appear this construction method, character encoding caused by the garbled problem is also more, here need to note: Once you see a byte array, you must think of coding problems .
The GetBytes (Charset Charset) method is often used:
Try { byte[] Bytearr = Strnew.getbytes ("UTF-8"new String (Bytearr, "UTF-8" Catch (unsupportedencodingexception e) {e.printstacktrace ();}
4) parameter is char array type
String (char[] value)
Allocates a new String so this it represents the sequence of characters currently contained in the character array argumen T.
It is worth noting that when we create a string using a character array, we use the Arrays.copyof method or the Arrays.copyofrange method. The two methods are to copy the contents of the original character array one at a to the character array in String. Creates a new string object, and then modifies the character array without affecting the newly created string.
Public String (char value[]) { this. Value = arrays.copyof (value, value.length);}
5) The parameter is StringBuilder or StringBuffer type
String (StringBuffer buffer)
Allocates a new string that contains the sequence of characters currently contained in the string buffer argument.
String (StringBuilder builder)
Allocates a new string that contains the sequence of characters currently contained in the String builder argument.
Personally think this way of construction method with less, if it is the two types, with the ToString method more convenient.
New StringBuilder (); Sbb.append ("Hello"). Append ("world"= sbb.tostring ();
Special thanks to the following blog posts:
The design of Java source code Analysis--string
Conversion of String to byte[] in Java
Java SE Basics (2)---how String is constructed