Update Time: 2018-1-6 21:20:39
For more information, see the online anthology: http://android.52fhy.com/java/index.html
String string creation
The first type is more space-saving. For string comparisons, if the direct use is to determine if the ==
address is the same, you need to use a method to determine whether the value is the same String.equals()
.
For strings: References to their objects are stored in the stack, and if the compilation period has been created (defined directly in double quotes), it is stored in a constant pool, which is stored in the heap if the run-time (new) is determined. For a string equal to equals, there is always only one copy in the constant pool, with multiple copies in the heap.
Direct assignment may create an object or do not create an object: If the string "ABC" does not exist in the Java string pool, it creates a string object ("abc") in the Java string pool, and if it already exists, str1 directly reference to The object in this string pool.
The new operation creates at least one object, or maybe two. Because the new keyword is used, a str2 string object is created in the heap (heap) whose value is "ABC". Also, if the "ABC" string does not exist in the Java string pool, it creates a string object ("abc") in the Java string pool.
The contents of the string cannot be changed. The string addition changes the point of the heap memory address.
Common methods of strings
- Length () string lengths
- ToCharArray () turns a string into a character array
- CharAt (int i) character of string at offset
- GetBytes (string decode) encodes the string into a byte sequence using the specified character set and stores the result in a new byte array. Null parameter to get an operating system default encoding format byte array
- IndexOf (String str) returns the index of the first occurrence of the specified substring in this string
- Trim () Returns a copy of the string, ignoring leading and trailing blanks
- substring (int start, int end) Returns a new string that is a substring of this string
- toLowerCase () converts all characters in this String to lowercase using the default locale's rules
- toUpperCase () converts all characters in this String to uppercase using the default locale's rules
- StartsWith (string prefix) tests whether this string starts with the specified prefix
- EndsWith (string suffix) tests whether this string ends with the specified suffix
- Replace (char OldChar, char Newchar) returns a new string that is obtained by replacing all OldChar that appear in this string with Newchar.
StringBuffer
StringBuffer is a string buffer, which is itself an action string, but unlike string, StringBuffer can be changed, and each modification does not reopen space as a string.
After processing the string through StringBuffer, we need to use new String(StringBuffer str)
or StringBuffer.toString()
convert the StringBuffer to the final string.
The following example shows that the StringBuffer is mutable:
publicclass StringDemo01 { publicstaticvoidmain(String[] args) { new StringBuffer("hello"); append(s); System.out.println(s.toString()); } publicstaticvoidappend(StringBuffer s) { s.append(" world"); }}
Run output:
Hello World
If you change to string:
publicclass StringDemo01 { publicstaticvoidmain(String[] args) { new String("hello"); append(s); System.out.println(s); } publicstaticvoidappend(String s) { " world"; }}
Run output:
Hello
Common methods of StringBuffer:
- Append (string str) concatenation string, similar to string operator + sign
- Insert (int offset, String str) inserts content at offset
- replace (int offset, int end, string str) Replacement string
- IndexOf (String str) returns the index of the first occurrence of the specified substring in this string
StringBuffer Usage Scenarios:
If you modify the string in a loop, use StringBuffer instead of string.
StringBuilder
1, a variable string sequence, similar to StringBuffer. This class is designed as a simple replacement for the StringBuffer. This class is recommended when used in string buffers by a single thread, faster than StringBuffer.
2, however, it is recommended to use StringBuffer when it comes to thread safety.
Since StringBuilder is similar to StringBuffer, there is no longer a detailed illustration.
Java notes: String explanations