1. Create objects
For direct string constants in Java programs, the JVM will use a string pool to save them. When a string direct constant is used for the first time, the JVM will put it into the string pool for caching. Under normal circumstances, string objects in the string pool will not be garbage collected. When the program needs to use the string again, the reference variable can directly point to the existing string in the string without re-creating a new string. The string object created using the new operation does not point to the object in the string pool , but you can use the intern() method to point it to the object in the string pool.
Copy the code code as follows:
public class StringDemo1 {
public static void main(String[] args){
String str1="abc";
String str2 = "abc";
String str3 =new String("abc");
System.out.println(str1==str2);//true
System.out.println(str1==str3);//false
}
}
FAQ
Copy the code code as follows:
String str3 =new String("abc");
How many objects were created?
Answer: Two
Copy the code code as follows:
String str ="ab"+"cd";
How many objects were created? Answer: One. "ab" and "cd" are constants placed in the string pool. Therefore, only one abcd string pool is created and the string abcd is saved in the string pool.
Copy the code code as follows:
public class StringDemo1 {
public static void main(String[] args){
String str1 = "ab";
String str2="cd";
String str3 ="ab"+"cd";//Create the object and add it to the string pool
String str4 =str1+str2;
String str5 =str1+"cd";
System.out.println(str3==str4);//false
System.out.println(str3==str5);//false
}
}
It can be seen from the above code: Only String objects created with quotation marks containing text can be added to the string pool. For the "+" connection expression containing the new object created by the new method, the new object generated by it will not be added to the string. in the pool.
But there is a situation that needs our attention:
Copy the code code as follows:
public class StringDemo1 {
private final static String str1 = "ab";
private final static String str2 = "cd";
public static void main(String[] args){
String str3 ="ab"+"cd";//Create the object and add it to the string pool
String str4 =str1+str2;
String str5 =str1+"cd";
System.out.println(str3==str4);//true
System.out.println(str3==str5);//true
}
}
Why is this? The reason is this, for constants. Its value is fixed and therefore can be determined at compile time.
Change the above code slightly and see what happens.
Copy the code code as follows:
public class StringDemo1 {
private final static String str1;
private final static String str2;
static{
str1="ab";
str2="cd";
}
public static void main(String[] args){
String str3 ="ab"+"cd";//Create the object and add it to the string pool
String str4 =str1+str2;
String str5 =str1+"cd";
System.out.println(str3==str4);//false
System.out.println(str3==str5);//false
}
}
Although str1 and str2 are defined as constants, they are assigned values immediately. Before the value of s is calculated, when they are assigned and what value they are assigned are variables, so their properties are the same as variables. Can only be created at runtime.
2. String method
Get method
•int length()
•char charAt(int index) gets a character based on position
•int indexOf(int ch) returns the position of the first occurrence of ch in the string
•int indexOf(int ch,int fromIndex) starts from the position specified by fromIndex and obtains the position of the first occurrence of ch in the string.
•int indexOf(String str)
•int indexOf(String str,int fromIndex)
•int lastIndexOf(int ch)
Judgment method
•boolean contains(String str) Another judgment method: if(str.index(str)!=-1)
•boolean startsWith(String str)
•boolean endsWith(String str)
•bolean isEmpty(String str)
•boolean equals(String str)
•boolean equalsIgnoreCase(String str);
Conversion method
•Convert character array to string
Constructor
1.String(char[] chs)
2.String(char[] chs,offset,count) converts part of the character array into a string.
static method
1.static String copyValueOf(char[] chs)
2.static String copyValueOf(char[] chs,int offset,int count)
3.static String valueOf(char[])
4.static String valueOf(char[] chs,int offset,int count)
• Convert a string into a character array
char[] toCharArray
•Convert character array to string
•Convert string to byte array
byte[] toBytes
replacement method
String replace(olderStr,newStr)
Cutting method
String split(regex)
Get substring[edit category]
String subString(begin)
String subString(begin,end) contains the head but not the tail
Convert string to uppercase and lowercase Android(10)
String toUpperCase()
String toLowerCase()
Remove spaces from both ends of string
String trim()
Compare two strings in natural order
int compareTo(String str)
3.String exercises
1. String flipping
Copy the code code as follows:
public class StringDemo2 {
public static void main(String[] args){
String str = "avdkfasjks";
reverseMethod_1(str);
}
public static void reverseMethod_1(String str){
for(int i=str.length();i>0;i--){
System.out.print(str.charAt(i-1));
}
}
}
2. Get the largest identical substring
Copy the code code as follows:
public class StringDemo2 {
public static void main(String[] args){
String str1 = "avdkfasjks";
String str2 = "ewavdrtte";
System.out.println(commonMaxSubstring(str1, str2));
}
public static String commonMaxSubstring(String str1,String str2){
int len = str1.length();
String str3 = null;
outer:
//i is the length of the substring
for(int i = len;i>0;i--){
//j is the substring of the substring
for(int j=0;j<len-i+1;j++){
str3=str1.substring(j,j+i);
if(str2.contains(str3))
break outer;
}
}
return str3;
}
}