String str1 = "hello"; //At this point str1 is on the stack, and hello is in the constant pool in the heap String str2 = new String("hello"); //The underlying call is the following program, which creates an array in heap to place the hello character in value public String(String original) { this.value = original.value; this.hash = original.hash; String str3 = new String("he")+new String("llo");
The following picture:
String str1 = "hello"; String str2 = "world" ; String str3 =" helloworld"; System. out. println(str3 ==(str1+str2)) ;//false System. out. println(str3 == ("hel1o"+ "world"));//false
As shown in the picture:
String str1 = new String("hello") ; String str2 = "hello"; System. out.print1n(strl == str2);//fa1sese String str3 = "he" + new String("llo"); System.out. println(str1 == str3);//false System .out.println(str2 == str3) ; String str4 = "he" + "11o"; System.out.println(str4 == str2) ;//true char[] array ={'h','e','l','l','o'}; String str5 = new String (array);//false System. out. println(str1 == str5) ;//false System. out. println(str2 == str5);//false System. out. println(str3 == str5) ;//false System. out. println(str4 == str5) ;//false
Pictured "
String str1 = new String("abcdef"); String str2 = "abcdef"; System.out.pritln(str1==str2);//false public static void main(String[] args){ String str3= new String("ab")+new String("cdef"); srt3.intern(); String str4 = "abcdef"; System.out.pritln(str3==str4);//true
As shown in the picture:
String str3 has created a new object in the heap and then calls the intern method, which automatically generates reference objects in the constant pool. So when str4 looks for it, it points to the new reference object, so the two addresses are the same, that is true.
public static void main(String[] args){ String string= new String("abcdef"); string.intern(); String str1 = "abcdef"; System.out.pritln(string==str1);//false
As shown in the picture:
When the second line of code is used, because it has been generated in the heap, the intern method cannot generate new reference objects in the constant pool, so when str1 goes to look up, it does not have, that is false.
2. String class methods:
1.length()
Confirm the length of the string;
2.isEmpty()
Determine whether the string is empty.
3.charAt(index)
Find the corresponding value of index index
public static void main(String[] args) { String string = "abcdef"; System.out.println(string.length()); //6 System.out.println(string.isEmpty());//false System.out.println(string.charAt(3));//d }
(4).getChars(int srcBegin, int srcEnd, char dst[], int dstBegin)
srcbegin: The first index of a string to be copied.
srcEnd: The last index of the string to be copied.
dst []: Target array.
dstBegin: The starting index of the target array.
public static void main(String[] args) { String string = "abcdef"; char [] chars = new char[5]; string.getChars(0,4,chars,0); System.out.println(chars); } }
(5)equals()
Compare the values of two strings
public static void main(String[] args) { String str1 = "abcdef"; String str2 = new String("abcdef"); System.out.println(str1.equals(str2));//true }
(6)compareTo
The two values are first converted into ASCII codes and then used as the ratio of difference. If they are greater than positive, they return 1, and negative returns - 1, they return 0.
public static void main(String[] args) { String str1 = "abcdef"; String str2 = "abcde"; System.out.println(str1.compareTo(str2));//1 }
(7)indexOf()
Returns the location where index first appears in the string. If not, return - 1.
public static void main(String[] args) { String str1 = "abcdcef"; int i = str1.indexOf('c',0); System.out.println(i); }
(8)substring(int beginIndex, int endIndex)
substr
beginIndex: Start subscript at interception
endIndex: The termination subscript does not contain itself.
public static void main(String[] args) { String str1 = "abcdcef"; String str2 = str1.substring(0,5); System.out.println(str2); }
(9)replace(char oldChar, char newChar)
Replacement string specifies the content.
public static void main(String[] args) { String str1 = "abc dcef"; String str2 = "abc"; System.out.println(str1.replace("bc","cc"));//accdcef System.out.println(str2.replace("bc","cc"));//acc }
(10)String[] split(String regex, int limit)
Converting strings to strings bounded by limit
public static void main(String[] args) { String str1 = "abc dcef"; String[] str2 = str1.split(" "); System.out.println(Arrays.toString(str2)); }
(11) toLowerCase converts strings to lowercase
toUpperCase converts strings to uppercase
public static void main(String[] args) { String str1 = "abcdef"; System.out.println(str1.toLowerCase()); System.out.println(str1.toUpperCase()); }
(12)toCharArray()
Converting strings to char arrays for storage
public static void main(String[] args) { String str1 = "abcdef"; char[] chars= str1.toCharArray(); System.out.println(Arrays.toString(chars)); }
(13)valueOf()
copyValueOf(char data[], int offset, int count)
char data []: an array to be copied
offset: the initial subscript for a copy
count: how many copies
public static void main(String[] args) { char[] chars= {'a','b','c','d'}; System.out.println(String.valueOf(chars)); System.out.println(String.copyValueOf(chars,0,3)); }