<> one , summary

String: character string , Use a pair of "" Cause to express .

* String Declare as final Of , Not inheritable
* String Realized Serializable Interface : Indicates that the string is serializable .
Realized Comparable Interface : express String You can compare the size
* String Internally defined final char[] value Used to store string data
* In a literal way ( Different from new Assign a value to a string , At this point, the string value is declared in the string constant pool ).
* String constant pool does not store the same content ( use String Class equals() compare , return true) Of the string .
<> two ,String The immutability of

* When a string is reassigned , The memory area assignment needs to be re specified , Cannot use original value Assign values .
* When a connection operation is performed on an existing string , You also need to reassign the memory area , Cannot use original value Assign values .
* When calling String Of replace() Method to modify the specified character or string , You also need to reassign the memory area , Cannot use original value Assign values . String s1 =
"abc";// The definition of literal quantity String s2 = "abc"; s1 = "hello"; System.out.println(s1 == s2);
// compare s1 and s2 Address value of System.out.println(s1);//hello System.out.println(s2);//abc System
.out.println("*****************"); String s3 = "abc"; s3 += "def"; System.out.
println(s3);//abcdef System.out.println(s2); System.out.println(
"*****************"); String s4 = "abc"; String s5 = s4.replace('a', 'm');
System.out.println(s4);//abc System.out.println(s5);//mbc

<> three ,String Different ways of instantiation

* By means of literal definition
* adopt new + The way of constructors // By means of literal definition : At this time s1 and s2 Data for javaEE Declared in the string constant pool in the method area . String s1 =
"javaEE"; String s2 = "javaEE"; // adopt new +
The way of constructors : At this time s3 and s4 Saved address values , It is the corresponding address value of data after opening up space in heap space . String s3 = new String("javaEE");
String s4= new String("javaEE"); System.out.println(s1 == s2);//true System.out.
println(s1 == s3);//false System.out.println(s1 == s4);//false System.out.
println(s3 == s4);//false
Interview questions : String s = new String("abc"); How to create objects , Several objects are created in memory ? Two :
One is in heap space new structure , The other is char[] Data in the corresponding constant pool :"abc"

<> four ,String Memory resolution of splicing

* The splicing result of constant and constant is in the constant pool .
* As long as one of them is a variable , The result is in the pile .
* If the result of splicing calls intern() method , The return value is in the constant pool . String s1 = "javaEE"; String s2 = "hadoop";
String s3= "javaEEhadoop"; String s4 = "javaEE" + "hadoop"; String s5 = s1 +
"hadoop"; String s6 = "javaEE" + s2; String s7 = s1 + s2; System.out.println(s3
== s4);//true System.out.println(s3 == s5);//false System.out.println(s3 == s6);
//false System.out.println(s3 == s7);//false System.out.println(s5 == s6);
//false System.out.println(s5 == s7);//false System.out.println(s6 == s7);
//false String s8 = s6.intern();// Return to the worthy s8 The value of the constant used already exists “javaEEhadoop” System.out.
println(s3 == s8);//true **************************** String s1 = "javaEEhadoop"
; String s2 = "javaEE"; String s3 = s2 + "hadoop"; System.out.println(s1 == s3);
//false final String s4 = "javaEE";//s4: constant String s5 = s4 + "hadoop"; System.out
.println(s1 == s5);//true

<> five ,String common method
int length(): Returns the length of the string : return value.length char charAt(int index): Returns the character at an index
return value[index] boolean isEmpty(): Determine whether it is an empty string :return value.length == 0 String
toLowerCase(): Use default locale , take String Converts the selected characters in to lowercase String toUpperCase(): Use default locale , take
String Converts the selected characters in to uppercase Stringtrim(): Returns a copy of a string , Ignore leading and trailing blanks boolean equals(Object obj)
: Compare whether the contents of the string are the same boolean equalsIgnoreCase(String anotherString): And equals The method is similar , ignore case
Stringconcat(String str): Concatenates the specified string to the end of this string . Equivalent to using “+” int compareTo(String
anotherString): Compare the size of two strings String substring(int beginIndex)
: Returns a new string , It is the slave of this string beginIndex Start to intercept to the last substring . Stringsubstring(int beginIndex, int
endIndex) : Returns a new string , It is this string from beginIndex Start intercepting to endIndex( Not included ) A substring of . boolean
endsWith(String suffix): Tests whether the string ends with the specified suffix boolean startsWith(String prefix)
: Tests whether the string starts with the specified prefix boolean startsWith(String prefix, int toffset)
: Tests whether the substring of this string starting at the specified index starts with the specified prefix boolean contains(CharSequence s): If and only if the string contains the specified char
Value sequence , return true int indexOf(String str): Returns the index of the first occurrence of the specified substring in the string int indexOf(String
str, int fromIndex): Returns the index of the first occurrence of the specified substring in the string , Starts with the specified index int lastIndexOf(String str)
: Returns the index of the rightmost occurrence of the specified substring in the string int lastIndexOf(String str, int fromIndex)
: Returns the index of the last occurrence of the specified substring in the string , Starts a reverse search from the specified index notes :indexOf and lastIndexOf Methods are returned if not found -1 replace :
Stringreplace(char oldChar, char newChar): Returns a new string , It's by using newChar Replace all occurrences in this string
oldChar Got it . Stringreplace(CharSequence target, CharSequence replacement)
: Replaces the substring of the literal target sequence matched by this string with the specified literal replacement sequence . StringreplaceAll(String regex, String
replacement): Use the given replacement Replaces the substring of the given regular expression that this string matches . String replaceFirst(
String regex, String replacement): Use the given replacement Replace this string to match the first substring of the given regular expression . matching
: boolean matches(String regex): Tells if the string matches the given regular expression . section : String[] split(String
regex): Splits the string according to the match of the given regular expression . String[] split(String regex, int limit)
: Splits the string by matching the given regular expression , No more than limit individual , If it exceeds , The rest is in the last element .
<> six ,String Transformation with other structures
6. String Transformation with other structures 6.1 And basic data types , Conversion between packaging classes String --> Basic data type , Packaging : Calling static methods of wrapper classes :
parseXxx(str) Basic data type , Packaging --> String: call String Overloaded valueOf(xxx) @Test public void
test1(){ String str1 = "123"; // int num = (int)str1;// FALSE int num = Integer.
parseInt(str1); String str2 = String.valueOf(num);//"123" String str3 = num + ""
; System.out.println(str1 == str3); } 6.2 Conversion between and character array String --> char[]: call String Of
toCharArray() char[] --> String: call String The constructors of @Test public void test2(){ String
str1= "abc123"; // subject : a21cb3 char[] charArray = str1.toCharArray(); for (int i =
0; i < charArray.length; i++) { System.out.println(charArray[i]); } char[] arr =
new char[]{'h','e','l','l','o'}; String str2 = new String(arr); System.out.
println(str2); } 6.3 Conversion between and byte array code :String --> byte[]: call String Of getBytes() decode :byte
[] --> String: call String The constructors of code : character string --> byte ( I can understand it ---> Incomprehensible binary data ) decode : The reverse process of coding , byte -->
character string ( Incomprehensible binary data ---> I can understand it explain : When decoding , The character set used for encoding must be the same as that used for decoding , Otherwise, there will be garbled code . @Test public
void test3() throws UnsupportedEncodingException { String str1 = "abc123 China ";
byte[] bytes = str1.getBytes();// Use the default character set , Code . System.out.println(Arrays.
toString(bytes)); byte[] gbks = str1.getBytes("gbk");// use gbk Character set . System.out.
println(Arrays.toString(gbks)); System.out.println("******************");
String str2= new String(bytes);// Use the default character set , Decode . System.out.println(str2);
String str3= new String(gbks); System.out.println(str3);// Garbled . reason : The encoding set is inconsistent with the decoding set !
String str4= new String(gbks, "gbk"); System.out.println(str4);
// There's no garbled code . reason : The coding set is consistent with the decoding set ! } 6.4 And StringBuffer,StringBuilder Conversion between String -->
StringBuffer,StringBuilder: call StringBuffer,StringBuilder constructor
StringBuffer,StringBuilder-->String:① call String constructor ;②StringBuffer,StringBuilder Of
<> seven , JVM Description of storage location of string constant pool in

* jdk 1.6 (jdk 6.0 ,java 6.0): The string constant pool is stored in the method area ( Permanent zone )
* jdk 1.7: The string constant pool is stored in heap space
* jdk 1.8: The string constant pool is stored in the method area ( Meta space )
<> eight ,String,StringBuffer,StringBuilder Comparison of the three

* String: Immutable character sequence ; Underlying use final char[] storage
* StringBuffer: Variable character sequence ; Thread safe , Low efficiency ; Underlying use char[] storage
* StringBuilder: Variable character sequence ;jdk5.0 New , Thread unsafe , efficient ; Underlying use char[] storage 2.
StringBuffer And StringBuilder Memory resolution of with StringBuffer take as an example : String str= new String();
//char[] value = new char[0]; String str1 = new String("abc");//char[] value =
new char[]{'a','b','c'}; StringBuffer sb1 = new StringBuffer();//char[] value =
new char[16]; The bottom layer creates a length of 16 Array of . System.out.println(sb1.length());// sb1.append('a'
);//value[0] = 'a'; sb1.append('b');//value[1] = 'b'; StringBuffer sb2 = new
StringBuffer("abc");//char[] value = new char["abc".length() + 16]; // problem 1.
System.out.println(sb2.length());//3 // problem 2. Capacity expansion : If you want to add data, the underlying array won't hold , Then you need to expand the underlying array .
By default , Expansion to the original capacity 2 times + 2, At the same time, the elements in the original array are copied to the new array . Guiding significance : It is suggested to use in the development :StringBuffer(int
capacity) or StringBuilder(int capacity) 3.
contrast String,StringBuffer,StringBuilder The implementation efficiency of the three From high to low :StringBuilder> StringBuffer >
String4.StringBuffer,StringBuilder The common methods in the design increase :append(xxx) Delete :delete(int start,int
end) change :setCharAt(int n ,char ch) / replace(int start, int end, String str) check :
charAt(int n ) insert :insert(int offset, xxx) length :length(); * ergodic :for() + charAt() /

©2020 ioDraw All rights reserved
Java exercises 1: Simply define a car class New photo album app launched by Microsoft : Using cross platform framework React Native to write Chapter two Fundamentals of programming Programming exercises pandas Time order of webpack Code segmentation import()Vue slot slot: Default slot , Named slot , Scope slot Fundamentals of data analysis ——R language ( Processing missing values )C++ Implementation of matrix meteor shower effect PostMan Advanced Usage , Interface MD5+Secret autograph Sign The 12th Blue Bridge Cup B group C/C++ Provincial competition